Skip to content

Commit dbb4af3

Browse files
author
thji
committed
feat: add root object
1 parent ce9fdcb commit dbb4af3

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public static String generateKey(Method method, Object[] args, String keyExpress
4242
if (expression == null) {
4343
return null;
4444
}
45+
// create root object
46+
MethodRootObject rootObject = new MethodRootObject(method, args);
4547

4648
try {
4749
String[] parameterNames = NAME_DISCOVERER.getParameterNames(method);
4850
if (parameterNames == null || args.length != parameterNames.length) {
4951
return null;
5052
}
51-
EvaluationContext context = new StandardEvaluationContext();
53+
EvaluationContext context = new StandardEvaluationContext(rootObject);
5254
for (int i = 0; i < args.length; i++) {
5355
context.setVariable(parameterNames[i], args[i]);
5456
}
@@ -62,6 +64,30 @@ public static String generateKey(Method method, Object[] args, String keyExpress
6264
}
6365
}
6466

67+
68+
private static class MethodRootObject {
69+
private final Method method;
70+
private final Object[] args;
71+
72+
private MethodRootObject(Method method, Object[] args) {
73+
this.method = method;
74+
this.args = args;
75+
}
76+
77+
public String getMethodName() {
78+
return method.getName();
79+
}
80+
81+
public Method getMethod() {
82+
return this.method;
83+
}
84+
85+
public Object[] getArgs() {
86+
return this.args;
87+
}
88+
89+
}
90+
6591
public static String replaceToExpression(Method method, String additionalSignature) {
6692
if (method == null || StringUtil.isEmpty(additionalSignature)) {
6793
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)