Search in sources :

Example 1 with MethodExecutor

use of org.springframework.expression.MethodExecutor 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 2 with MethodExecutor

use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.

the class Spr7538Tests method repro.

@Ignore
@Test
public void repro() throws Exception {
    AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy();
    BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setTypeConverter(converter);
    List<Foo> arguments = new ArrayList<>();
    // !!!! With the below line commented you'll get NPE. Uncomment and everything is OK!
    //arguments.add(new Foo());
    List<TypeDescriptor> paramDescriptors = new ArrayList<>();
    Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class);
    paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, 0)));
    List<TypeDescriptor> argumentTypes = new ArrayList<>();
    argumentTypes.add(TypeDescriptor.forObject(arguments));
    ReflectiveMethodResolver resolver = new ReflectiveMethodResolver();
    MethodExecutor executor = resolver.resolve(context, target, "checkCompleteness", argumentTypes);
    Object result = executor.execute(context, target, arguments);
    System.out.println("Result: " + result);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) MethodExecutor(org.springframework.expression.MethodExecutor) MethodParameter(org.springframework.core.MethodParameter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with MethodExecutor

use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.

the class MethodReference method getValueInternal.

private TypedValue getValueInternal(EvaluationContext evaluationContext, Object value, TypeDescriptor targetType, Object[] arguments) {
    List<TypeDescriptor> argumentTypes = getArgumentTypes(arguments);
    if (value == null) {
        throwIfNotNullSafe(argumentTypes);
        return TypedValue.NULL;
    }
    MethodExecutor executorToUse = getCachedExecutor(evaluationContext, value, targetType, argumentTypes);
    if (executorToUse != null) {
        try {
            return executorToUse.execute(evaluationContext, value, arguments);
        } catch (AccessException ex) {
            // Two reasons this can occur:
            // 1. the method invoked actually threw a real exception
            // 2. the method invoked was not passed the arguments it expected and
            //    has become 'stale'
            // In the first case we should not retry, in the second case we should see
            // if there is a better suited method.
            // To determine the situation, the AccessException will contain a cause.
            // If the cause is an InvocationTargetException, a user exception was
            // thrown inside the method. Otherwise the method could not be invoked.
            throwSimpleExceptionIfPossible(value, ex);
            // At this point we know it wasn't a user problem so worth a retry if a
            // better candidate can be found.
            this.cachedExecutor = null;
        }
    }
    // either there was no accessor or it no longer existed
    executorToUse = findAccessorForMethod(this.name, argumentTypes, value, evaluationContext);
    this.cachedExecutor = new CachedMethodExecutor(executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
    try {
        return executorToUse.execute(evaluationContext, value, arguments);
    } catch (AccessException ex) {
        // Same unwrapping exception handling as above in above catch block
        throwSimpleExceptionIfPossible(value, ex);
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION, this.name, value.getClass().getName(), ex.getMessage());
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodExecutor(org.springframework.expression.spel.support.ReflectiveMethodExecutor) MethodExecutor(org.springframework.expression.MethodExecutor)

Example 4 with MethodExecutor

use of org.springframework.expression.MethodExecutor 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 5 with MethodExecutor

use of org.springframework.expression.MethodExecutor 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)

Aggregations

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