use of cn.taketoday.expression.MethodResolver in project today-infrastructure by TAKETODAY.
the class SpelReproTests method SPR9495.
@Test
void SPR9495() {
SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);
StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");
context.setVariable("enumType", ABC.class);
Object result = spel.getValue(context);
assertThat(result).isNotNull();
assertThat(result.getClass().isArray()).isTrue();
assertThat(Array.get(result, 0)).isEqualTo(ABC.A);
assertThat(Array.get(result, 1)).isEqualTo(ABC.B);
assertThat(Array.get(result, 2)).isEqualTo(ABC.C);
context.addMethodResolver(new MethodResolver() {
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
return (context1, target, arguments) -> {
try {
Method method = XYZ.class.getMethod("values");
Object value = method.invoke(target, arguments);
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
} catch (Exception ex) {
throw new AccessException(ex.getMessage(), ex);
}
};
}
});
result = spel.getValue(context);
assertThat(result).isNotNull();
assertThat(result.getClass().isArray()).isTrue();
assertThat(Array.get(result, 0)).isEqualTo(XYZ.X);
assertThat(Array.get(result, 1)).isEqualTo(XYZ.Y);
assertThat(Array.get(result, 2)).isEqualTo(XYZ.Z);
}
Aggregations