Search in sources :

Example 1 with ReflectiveMethodResolver

use of cn.taketoday.expression.spel.support.ReflectiveMethodResolver in project today-infrastructure by TAKETODAY.

the class SpelReproTests method varargsAndPrimitives_SPR8174.

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

Example 2 with ReflectiveMethodResolver

use of cn.taketoday.expression.spel.support.ReflectiveMethodResolver in project today-infrastructure by TAKETODAY.

the class SpelReproTests method wideningPrimitiveConversion_SPR8224.

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

        public int getX(long i) {
            return 10;
        }
    }
    final Integer INTEGER_VALUE = 7;
    WideningPrimitiveConversion target = new WideningPrimitiveConversion();
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.fromObject(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);
    assertThat(actual).isEqualTo(compiler);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(cn.taketoday.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 3 with ReflectiveMethodResolver

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

Example 4 with ReflectiveMethodResolver

use of cn.taketoday.expression.spel.support.ReflectiveMethodResolver in project today-framework by TAKETODAY.

the class SpelReproTests method wideningPrimitiveConversion_SPR8224.

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

        public int getX(long i) {
            return 10;
        }
    }
    final Integer INTEGER_VALUE = 7;
    WideningPrimitiveConversion target = new WideningPrimitiveConversion();
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.fromObject(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);
    assertThat(actual).isEqualTo(compiler);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(cn.taketoday.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 5 with ReflectiveMethodResolver

use of cn.taketoday.expression.spel.support.ReflectiveMethodResolver in project today-framework by TAKETODAY.

the class SpelReproTests method varargsAndPrimitives_SPR8174.

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

Aggregations

ReflectiveMethodResolver (cn.taketoday.expression.spel.support.ReflectiveMethodResolver)8 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)8 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Test (org.junit.jupiter.api.Test)8 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 EvaluationContext (cn.taketoday.expression.EvaluationContext)6 MethodExecutor (cn.taketoday.expression.MethodExecutor)6 Expression (cn.taketoday.expression.Expression)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2 MethodResolver (cn.taketoday.expression.MethodResolver)2 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)2 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)2 Method (java.lang.reflect.Method)2