use of org.springframework.expression.MethodResolver in project spring-framework by spring-projects.
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();
assertEquals(1, methodResolvers.size());
MethodResolver dummy = new DummyMethodResolver();
ctx.addMethodResolver(dummy);
assertEquals(2, ctx.getMethodResolvers().size());
List<MethodResolver> copy = new ArrayList<>();
copy.addAll(ctx.getMethodResolvers());
assertTrue(ctx.removeMethodResolver(dummy));
assertFalse(ctx.removeMethodResolver(dummy));
assertEquals(1, ctx.getMethodResolvers().size());
ctx.setMethodResolvers(copy);
assertEquals(2, ctx.getMethodResolvers().size());
}
use of org.springframework.expression.MethodResolver in project spring-framework by spring-projects.
the class SpelReproTests method SPR9495.
@Test
public void SPR9495() throws Exception {
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);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(ABC.A, Array.get(result, 0));
assertEquals(ABC.B, Array.get(result, 1));
assertEquals(ABC.C, Array.get(result, 2));
context.addMethodResolver(new MethodResolver() {
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
return new MethodExecutor() {
@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
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);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(XYZ.X, Array.get(result, 0));
assertEquals(XYZ.Y, Array.get(result, 1));
assertEquals(XYZ.Z, Array.get(result, 2));
}
use of org.springframework.expression.MethodResolver in project spring-framework by spring-projects.
the class EvaluationTests method customMethodFilter.
/**
* Verifies behavior requested in SPR-9621.
*/
@Test
public void customMethodFilter() throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext();
// Register a custom MethodResolver...
List<MethodResolver> customResolvers = new ArrayList<>();
customResolvers.add(new CustomMethodResolver());
context.setMethodResolvers(customResolvers);
// or simply...
// context.setMethodResolvers(new ArrayList<MethodResolver>());
// Register a custom MethodFilter...
MethodFilter filter = new CustomMethodFilter();
try {
context.registerMethodFilter(String.class, filter);
fail("should have failed");
} catch (IllegalStateException ise) {
assertEquals("Method filter cannot be set as the reflective method resolver is not in use", ise.getMessage());
}
}
use of org.springframework.expression.MethodResolver in project spring-framework by spring-projects.
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
public 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", new Class<?>[] { 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);
assertEquals(-255, result.intValue());
}
Aggregations