Skip to content

feat: add root object #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ public static String generateKey(Method method, Object[] args, String keyExpress
return null;
}


try {
String[] parameterNames = NAME_DISCOVERER.getParameterNames(method);
if (parameterNames == null || args.length != parameterNames.length) {
return null;
}
EvaluationContext context = new StandardEvaluationContext();

MethodRootObject rootObject = new MethodRootObject(method, args);

EvaluationContext context = new StandardEvaluationContext(rootObject);
for (int i = 0; i < args.length; i++) {
context.setVariable(parameterNames[i], args[i]);
}
Expand All @@ -62,6 +66,33 @@ public static String generateKey(Method method, Object[] args, String keyExpress
}
}

/**
* the root object used during the expression evaluation. It contains the method and its
* arguments.
*/
private static class MethodRootObject {
private final Method method;
private final Object[] args;

private MethodRootObject(Method method, Object[] args) {
this.method = method;
this.args = args;
}

public String getMethodName() {
return method.getName();
}

public Method getMethod() {
return this.method;
}

public Object[] getArgs() {
return this.args;
}

}

public static String replaceToExpression(Method method, String additionalSignature) {
if (method == null || StringUtil.isEmpty(additionalSignature)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,42 @@ static Stream<Arguments> generateKeyArgs() throws NoSuchMethodException {
Foo1.class),
"T(io.arex.inst.dynamic.common.ExpressionParseUtilTest).getMapKey(#f1.f1, #f1.f2)",
(Predicate<String>) "getMapKey-p1-1"::equals
),
Arguments.of(
new Object[]{
new Foo2("p1", 1),
new Foo2("p2", 2),
new Foo1(new Foo2("p3", 3))
},
ExpressionParseUtilTest.class.getDeclaredMethod("testParseMethodKey3", Foo2.class,
Foo2.class,
Foo1.class),
"#root.methodName + #f1.getF2() + #f2.getF2().toString() + #f3.getFoo2().f1",
(Predicate<String>) "testParseMethodKey312p3"::equals
),
Arguments.of(
new Object[]{
new Foo2("p1", 1),
new Foo2("p2", 2),
new Foo1(new Foo2("p3", 3))
},
ExpressionParseUtilTest.class.getDeclaredMethod("testParseMethodKey3", Foo2.class,
Foo2.class,
Foo1.class),
"#root.method.name + #f1.getF2() + #f2.getF2().toString() + #f3.getFoo2().f1",
(Predicate<String>) "testParseMethodKey312p3"::equals
),
Arguments.of(
new Object[]{
new Foo2("p1", 1),
new Foo2("p2", 2),
new Foo1(new Foo2("p3", 3))
},
ExpressionParseUtilTest.class.getDeclaredMethod("testParseMethodKey3", Foo2.class,
Foo2.class,
Foo1.class),
"#root.args[0].getF2().toString + #f1.getF2() + #f2.getF2().toString() + #f3.getFoo2().f1",
(Predicate<String>) "112p3"::equals
)
);
}
Expand Down