Search in sources :

Example 1 with ReflectiveMethodResolver

use of org.springframework.expression.spel.support.ReflectiveMethodResolver in project spring-framework by spring-projects.

the class SpelReproTests method conversionPriority_8224.

/**
	 * Test whether {@link ReflectiveMethodResolver} follows Java Method Invocation
	 * Conversion order. And more precisely that widening reference conversion is 'higher'
	 * than a unboxing conversion.
	 */
@Test
public void conversionPriority_8224() throws Exception {
    @SuppressWarnings("unused")
    class ConversionPriority1 {

        public int getX(Number i) {
            return 20;
        }

        public int getX(int i) {
            return 10;
        }
    }
    @SuppressWarnings("unused")
    class ConversionPriority2 {

        public int getX(int i) {
            return 10;
        }

        public int getX(Number i) {
            return 20;
        }
    }
    final Integer INTEGER = Integer.valueOf(7);
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.forObject(new Integer(42)));
    ConversionPriority1 target = new ConversionPriority1();
    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    // MethodInvoker chooses getX(int i) when passing Integer
    final int actual = (Integer) me.execute(emptyEvalContext, target, new Integer(42)).getValue();
    // Compiler chooses getX(Number i) when passing Integer
    final int compiler = target.getX(INTEGER);
    // Fails!
    assertEquals(compiler, actual);
    ConversionPriority2 target2 = new ConversionPriority2();
    MethodExecutor me2 = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target2, "getX", args);
    // MethodInvoker chooses getX(int i) when passing Integer
    int actual2 = (Integer) me2.execute(emptyEvalContext, target2, new Integer(42)).getValue();
    // Compiler chooses getX(Number i) when passing Integer
    int compiler2 = target2.getX(INTEGER);
    // Fails!
    assertEquals(compiler2, actual2);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(org.springframework.expression.MethodExecutor) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 2 with ReflectiveMethodResolver

use of org.springframework.expression.spel.support.ReflectiveMethodResolver 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)

Example 3 with ReflectiveMethodResolver

use of org.springframework.expression.spel.support.ReflectiveMethodResolver in project spring-framework by spring-projects.

the class SpelReproTests method varargsAndPrimitives_SPR8174.

@Test
public void varargsAndPrimitives_SPR8174() throws Exception {
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.forObject(34L));
    ReflectionUtil<Integer> ru = new ReflectionUtil<>();
    MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args);
    args.set(0, TypeDescriptor.forObject(23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45);
    args.set(0, TypeDescriptor.forObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45f);
    args.set(0, TypeDescriptor.forObject(23d));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23d);
    args.set(0, TypeDescriptor.forObject((short) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (short) 23);
    args.set(0, TypeDescriptor.forObject(23L));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23L);
    args.set(0, TypeDescriptor.forObject((char) 65));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (char) 65);
    args.set(0, TypeDescriptor.forObject((byte) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (byte) 23);
    args.set(0, TypeDescriptor.forObject(true));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, true);
    // trickier:
    args.set(0, TypeDescriptor.forObject(12));
    args.add(TypeDescriptor.forObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args);
    me.execute(emptyEvalContext, ru, 12, 23f);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(org.springframework.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 4 with ReflectiveMethodResolver

use of org.springframework.expression.spel.support.ReflectiveMethodResolver in project spring-framework by spring-projects.

the class SpelReproTests method wideningPrimitiveConversion_8224.

/**
	 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
	 * method accepting 'long' is ok.
	 */
@Test
public void wideningPrimitiveConversion_8224() throws Exception {
    class WideningPrimitiveConversion {

        public int getX(long i) {
            return 10;
        }
    }
    final Integer INTEGER_VALUE = Integer.valueOf(7);
    WideningPrimitiveConversion target = new WideningPrimitiveConversion();
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.forObject(INTEGER_VALUE));
    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();
    final int compiler = target.getX(INTEGER_VALUE);
    assertEquals(compiler, actual);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(org.springframework.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Aggregations

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