Search in sources :

Example 1 with MethodResolver

use of cn.taketoday.expression.MethodResolver in project today-infrastructure by TAKETODAY.

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();
    assertThat(methodResolvers.size()).isEqualTo(1);
    MethodResolver dummy = new DummyMethodResolver();
    ctx.addMethodResolver(dummy);
    assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
    List<MethodResolver> copy = new ArrayList<>(ctx.getMethodResolvers());
    assertThat(ctx.removeMethodResolver(dummy)).isTrue();
    assertThat(ctx.removeMethodResolver(dummy)).isFalse();
    assertThat(ctx.getMethodResolvers().size()).isEqualTo(1);
    ctx.setMethodResolvers(copy);
    assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) MethodResolver(cn.taketoday.expression.MethodResolver) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 2 with MethodResolver

use of cn.taketoday.expression.MethodResolver in project today-framework by TAKETODAY.

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();
    assertThat(methodResolvers.size()).isEqualTo(1);
    MethodResolver dummy = new DummyMethodResolver();
    ctx.addMethodResolver(dummy);
    assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
    List<MethodResolver> copy = new ArrayList<>(ctx.getMethodResolvers());
    assertThat(ctx.removeMethodResolver(dummy)).isTrue();
    assertThat(ctx.removeMethodResolver(dummy)).isFalse();
    assertThat(ctx.getMethodResolvers().size()).isEqualTo(1);
    ctx.setMethodResolvers(copy);
    assertThat(ctx.getMethodResolvers().size()).isEqualTo(2);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) MethodResolver(cn.taketoday.expression.MethodResolver) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with MethodResolver

use of cn.taketoday.expression.MethodResolver in project today-framework by TAKETODAY.

the class SpelReproTests method SPR9495.

@Test
void SPR9495() {
    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);
    assertThat(result).isNotNull();
    assertThat(result.getClass().isArray()).isTrue();
    assertThat(Array.get(result, 0)).isEqualTo(ABC.A);
    assertThat(Array.get(result, 1)).isEqualTo(ABC.B);
    assertThat(Array.get(result, 2)).isEqualTo(ABC.C);
    context.addMethodResolver(new MethodResolver() {

        @Override
        public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
            return (context1, target, arguments) -> {
                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);
    assertThat(result).isNotNull();
    assertThat(result.getClass().isArray()).isTrue();
    assertThat(Array.get(result, 0)).isEqualTo(XYZ.X);
    assertThat(Array.get(result, 1)).isEqualTo(XYZ.Y);
    assertThat(Array.get(result, 2)).isEqualTo(XYZ.Z);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) MethodResolver(cn.taketoday.expression.MethodResolver) Method(java.lang.reflect.Method) EvaluationException(cn.taketoday.expression.EvaluationException) ExpressionException(cn.taketoday.expression.ExpressionException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) AccessException(cn.taketoday.expression.AccessException) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) AccessException(cn.taketoday.expression.AccessException) TypeDescriptor(cn.taketoday.core.TypeDescriptor) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) MethodExecutor(cn.taketoday.expression.MethodExecutor) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) MethodParameter(cn.taketoday.core.MethodParameter) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 4 with MethodResolver

use of cn.taketoday.expression.MethodResolver 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 5 with MethodResolver

use of cn.taketoday.expression.MethodResolver in project today-infrastructure 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)

Aggregations

MethodResolver (cn.taketoday.expression.MethodResolver)6 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)6 Test (org.junit.jupiter.api.Test)6 Expression (cn.taketoday.expression.Expression)4 ExpressionParser (cn.taketoday.expression.ExpressionParser)4 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)4 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)4 ReflectiveMethodResolver (cn.taketoday.expression.spel.support.ReflectiveMethodResolver)4 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 MethodParameter (cn.taketoday.core.MethodParameter)2 TypeDescriptor (cn.taketoday.core.TypeDescriptor)2 AccessException (cn.taketoday.expression.AccessException)2 EvaluationContext (cn.taketoday.expression.EvaluationContext)2 EvaluationException (cn.taketoday.expression.EvaluationException)2 ExpressionException (cn.taketoday.expression.ExpressionException)2 MethodExecutor (cn.taketoday.expression.MethodExecutor)2 TypedValue (cn.taketoday.expression.TypedValue)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2