Search in sources :

Example 11 with AccessException

use of cn.taketoday.expression.AccessException in project today-infrastructure 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 12 with AccessException

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

the class ReflectiveConstructorExecutor method execute.

@Override
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
    try {
        ReflectionHelper.convertArguments(context.getTypeConverter(), arguments, this.ctor, this.varargsPosition);
        if (this.ctor.isVarArgs()) {
            arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.ctor.getParameterTypes(), arguments);
        }
        ReflectionUtils.makeAccessible(this.ctor);
        return new TypedValue(this.ctor.newInstance(arguments));
    } catch (Exception ex) {
        throw new AccessException("Problem invoking constructor: " + this.ctor, ex);
    }
}
Also used : AccessException(cn.taketoday.expression.AccessException) AccessException(cn.taketoday.expression.AccessException) TypedValue(cn.taketoday.expression.TypedValue)

Aggregations

AccessException (cn.taketoday.expression.AccessException)12 MethodParameter (cn.taketoday.core.MethodParameter)8 TypeDescriptor (cn.taketoday.core.TypeDescriptor)8 EvaluationException (cn.taketoday.expression.EvaluationException)6 TypedValue (cn.taketoday.expression.TypedValue)6 Expression (cn.taketoday.expression.Expression)4 TypeConverter (cn.taketoday.expression.TypeConverter)4 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)4 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 Nullable (cn.taketoday.lang.Nullable)4 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 Test (org.junit.jupiter.api.Test)4 EvaluationContext (cn.taketoday.expression.EvaluationContext)2 ExpressionException (cn.taketoday.expression.ExpressionException)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2 MethodExecutor (cn.taketoday.expression.MethodExecutor)2 MethodFilter (cn.taketoday.expression.MethodFilter)2 MethodResolver (cn.taketoday.expression.MethodResolver)2