Skip to content

Commit 3efe872

Browse files
foodjththji
and
thji
authored
feat: add root object (#605)
* feat: add root object * feat: code style --------- Co-authored-by: thji <[email protected]>
1 parent 5854c29 commit 3efe872

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

arex-instrumentation/dynamic/arex-dynamic-common/src/main/java/io/arex/inst/dynamic/common/ExpressionParseUtil.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ public static String generateKey(Method method, Object[] args, String keyExpress
4343
return null;
4444
}
4545

46+
4647
try {
4748
String[] parameterNames = NAME_DISCOVERER.getParameterNames(method);
4849
if (parameterNames == null || args.length != parameterNames.length) {
4950
return null;
5051
}
51-
EvaluationContext context = new StandardEvaluationContext();
52+
53+
MethodRootObject rootObject = new MethodRootObject(method, args);
54+
55+
EvaluationContext context = new StandardEvaluationContext(rootObject);
5256
for (int i = 0; i < args.length; i++) {
5357
context.setVariable(parameterNames[i], args[i]);
5458
}
@@ -62,6 +66,33 @@ public static String generateKey(Method method, Object[] args, String keyExpress
6266
}
6367
}
6468

69+
/**
70+
* the root object used during the expression evaluation. It contains the method and its
71+
* arguments.
72+
*/
73+
private static class MethodRootObject {
74+
private final Method method;
75+
private final Object[] args;
76+
77+
private MethodRootObject(Method method, Object[] args) {
78+
this.method = method;
79+
this.args = args;
80+
}
81+
82+
public String getMethodName() {
83+
return method.getName();
84+
}
85+
86+
public Method getMethod() {
87+
return this.method;
88+
}
89+
90+
public Object[] getArgs() {
91+
return this.args;
92+
}
93+
94+
}
95+
6596
public static String replaceToExpression(Method method, String additionalSignature) {
6697
if (method == null || StringUtil.isEmpty(additionalSignature)) {
6798
return null;

arex-instrumentation/dynamic/arex-dynamic-common/src/test/java/io/arex/inst/dynamic/common/ExpressionParseUtilTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ static Stream<Arguments> generateKeyArgs() throws NoSuchMethodException {
114114
Foo1.class),
115115
"T(io.arex.inst.dynamic.common.ExpressionParseUtilTest).getMapKey(#f1.f1, #f1.f2)",
116116
(Predicate<String>) "getMapKey-p1-1"::equals
117+
),
118+
Arguments.of(
119+
new Object[]{
120+
new Foo2("p1", 1),
121+
new Foo2("p2", 2),
122+
new Foo1(new Foo2("p3", 3))
123+
},
124+
ExpressionParseUtilTest.class.getDeclaredMethod("testParseMethodKey3", Foo2.class,
125+
Foo2.class,
126+
Foo1.class),
127+
"#root.methodName + #f1.getF2() + #f2.getF2().toString() + #f3.getFoo2().f1",
128+
(Predicate<String>) "testParseMethodKey312p3"::equals
129+
),
130+
Arguments.of(
131+
new Object[]{
132+
new Foo2("p1", 1),
133+
new Foo2("p2", 2),
134+
new Foo1(new Foo2("p3", 3))
135+
},
136+
ExpressionParseUtilTest.class.getDeclaredMethod("testParseMethodKey3", Foo2.class,
137+
Foo2.class,
138+
Foo1.class),
139+
"#root.method.name + #f1.getF2() + #f2.getF2().toString() + #f3.getFoo2().f1",
140+
(Predicate<String>) "testParseMethodKey312p3"::equals
141+
),
142+
Arguments.of(
143+
new Object[]{
144+
new Foo2("p1", 1),
145+
new Foo2("p2", 2),
146+
new Foo1(new Foo2("p3", 3))
147+
},
148+
ExpressionParseUtilTest.class.getDeclaredMethod("testParseMethodKey3", Foo2.class,
149+
Foo2.class,
150+
Foo1.class),
151+
"#root.args[0].getF2().toString + #f1.getF2() + #f2.getF2().toString() + #f3.getFoo2().f1",
152+
(Predicate<String>) "112p3"::equals
117153
)
118154
);
119155
}

0 commit comments

Comments
 (0)