Search in sources :

Example 1 with MethodResolver

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());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with MethodResolver

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));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) Method(java.lang.reflect.Method) ExpressionException(org.springframework.expression.ExpressionException) EvaluationException(org.springframework.expression.EvaluationException) ExpectedException(org.junit.rules.ExpectedException) AccessException(org.springframework.expression.AccessException) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) MethodExecutor(org.springframework.expression.MethodExecutor) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodParameter(org.springframework.core.MethodParameter) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 3 with MethodResolver

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());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) MethodFilter(org.springframework.expression.MethodFilter) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with MethodResolver

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());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MethodResolver(org.springframework.expression.MethodResolver) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 MethodResolver (org.springframework.expression.MethodResolver)4 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)4 ArrayList (java.util.ArrayList)3 Method (java.lang.reflect.Method)2 Expression (org.springframework.expression.Expression)2 ExpressionParser (org.springframework.expression.ExpressionParser)2 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)2 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)2 ReflectiveMethodResolver (org.springframework.expression.spel.support.ReflectiveMethodResolver)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ExpectedException (org.junit.rules.ExpectedException)1 MethodParameter (org.springframework.core.MethodParameter)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 AccessException (org.springframework.expression.AccessException)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 EvaluationException (org.springframework.expression.EvaluationException)1 ExpressionException (org.springframework.expression.ExpressionException)1 MethodExecutor (org.springframework.expression.MethodExecutor)1 MethodFilter (org.springframework.expression.MethodFilter)1