use of cn.taketoday.expression.MethodResolver in project today-infrastructure by TAKETODAY.
the class MethodInvocationTests method testAddingMethodResolvers.
@Test
public void testAddingMethodResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective method accessor is the only one by default
List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
assertThat(methodResolvers.size()).isEqualTo(1);
MethodResolver dummy = new DummyMethodResolver();
ctx.addMethodResolver(dummy);
assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
List<MethodResolver> copy = new ArrayList<>(ctx.getMethodResolvers());
assertThat(ctx.removeMethodResolver(dummy)).isTrue();
assertThat(ctx.removeMethodResolver(dummy)).isFalse();
assertThat(ctx.getMethodResolvers().size()).isEqualTo(1);
ctx.setMethodResolvers(copy);
assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
}
use of cn.taketoday.expression.MethodResolver in project today-framework by TAKETODAY.
the class MethodInvocationTests method testAddingMethodResolvers.
@Test
public void testAddingMethodResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective method accessor is the only one by default
List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
assertThat(methodResolvers.size()).isEqualTo(1);
MethodResolver dummy = new DummyMethodResolver();
ctx.addMethodResolver(dummy);
assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
List<MethodResolver> copy = new ArrayList<>(ctx.getMethodResolvers());
assertThat(ctx.removeMethodResolver(dummy)).isTrue();
assertThat(ctx.removeMethodResolver(dummy)).isFalse();
assertThat(ctx.getMethodResolvers().size()).isEqualTo(1);
ctx.setMethodResolvers(copy);
assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
}
use of cn.taketoday.expression.MethodResolver in project today-framework 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);
}
use of cn.taketoday.expression.MethodResolver in project today-framework by TAKETODAY.
the class SpelReproTests method customStaticFunctions_SPR9038.
/**
* Test the ability to subclass the ReflectiveMethodResolver and change how it
* determines the set of methods for a type.
*/
@Test
void customStaticFunctions_SPR9038() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
List<MethodResolver> methodResolvers = new ArrayList<>();
methodResolvers.add(new ReflectiveMethodResolver() {
@Override
protected Method[] getMethods(Class<?> type) {
try {
return new Method[] { Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE) };
} catch (NoSuchMethodException ex) {
return new Method[0];
}
}
});
context.setMethodResolvers(methodResolvers);
Expression expression = parser.parseExpression("parseInt('-FF', 16)");
Integer result = expression.getValue(context, "", Integer.class);
assertThat(result.intValue()).isEqualTo(-255);
}
use of cn.taketoday.expression.MethodResolver in project today-infrastructure by TAKETODAY.
the class SpelReproTests method customStaticFunctions_SPR9038.
/**
* Test the ability to subclass the ReflectiveMethodResolver and change how it
* determines the set of methods for a type.
*/
@Test
void customStaticFunctions_SPR9038() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
List<MethodResolver> methodResolvers = new ArrayList<>();
methodResolvers.add(new ReflectiveMethodResolver() {
@Override
protected Method[] getMethods(Class<?> type) {
try {
return new Method[] { Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE) };
} catch (NoSuchMethodException ex) {
return new Method[0];
}
}
});
context.setMethodResolvers(methodResolvers);
Expression expression = parser.parseExpression("parseInt('-FF', 16)");
Integer result = expression.getValue(context, "", Integer.class);
assertThat(result.intValue()).isEqualTo(-255);
}
Aggregations