Search in sources :

Example 26 with TypedValue

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

the class ReflectiveMethodExecutor method execute.

@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
    try {
        this.argumentConversionOccurred = ReflectionHelper.convertArguments(context.getTypeConverter(), arguments, this.originalMethod, this.varargsPosition);
        if (this.originalMethod.isVarArgs()) {
            arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.originalMethod.getParameterTypes(), arguments);
        }
        ReflectionUtils.makeAccessible(this.methodToInvoke);
        Object value = this.methodToInvoke.invoke(target, arguments);
        return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.originalMethod, -1)).narrow(value));
    } catch (Exception ex) {
        throw new AccessException("Problem invoking method: " + this.methodToInvoke, ex);
    }
}
Also used : AccessException(cn.taketoday.expression.AccessException) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) AccessException(cn.taketoday.expression.AccessException) TypedValue(cn.taketoday.expression.TypedValue)

Example 27 with TypedValue

use of cn.taketoday.expression.TypedValue 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 28 with TypedValue

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            Object result = compiledAst.getValue(rootObject, context);
            if (expectedResultType != null) {
                return ExpressionUtils.convertTypedValue(context, new TypedValue(result), expectedResultType);
            } else {
                return (T) result;
            }
        } catch (Throwable ex) {
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.compiledAst = null;
                this.interpretedCount.set(0);
            } else {
                // Running in SpelCompilerMode.immediate mode - propagate exception to caller
                throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
            }
        }
    }
    ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);
    TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
    checkCompile(expressionState);
    return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) TypedValue(cn.taketoday.expression.TypedValue) Nullable(cn.taketoday.lang.Nullable)

Example 29 with TypedValue

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(@Nullable Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            Object result = compiledAst.getValue(rootObject, getEvaluationContext());
            if (expectedResultType == null) {
                return (T) result;
            } else {
                return ExpressionUtils.convertTypedValue(getEvaluationContext(), new TypedValue(result), expectedResultType);
            }
        } catch (Throwable ex) {
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.compiledAst = null;
                this.interpretedCount.set(0);
            } else {
                // Running in SpelCompilerMode.immediate mode - propagate exception to caller
                throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
            }
        }
    }
    ExpressionState expressionState = new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration);
    TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
    checkCompile(expressionState);
    return ExpressionUtils.convertTypedValue(expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) TypedValue(cn.taketoday.expression.TypedValue) Nullable(cn.taketoday.lang.Nullable)

Example 30 with TypedValue

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            EvaluationContext context = getEvaluationContext();
            Object result = compiledAst.getValue(context.getRootObject().getValue(), context);
            if (expectedResultType == null) {
                return (T) result;
            } else {
                return ExpressionUtils.convertTypedValue(getEvaluationContext(), new TypedValue(result), expectedResultType);
            }
        } catch (Throwable ex) {
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.compiledAst = null;
                this.interpretedCount.set(0);
            } else {
                // Running in SpelCompilerMode.immediate mode - propagate exception to caller
                throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
            }
        }
    }
    ExpressionState expressionState = new ExpressionState(getEvaluationContext(), this.configuration);
    TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
    checkCompile(expressionState);
    return ExpressionUtils.convertTypedValue(expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) TypedValue(cn.taketoday.expression.TypedValue) Nullable(cn.taketoday.lang.Nullable)

Aggregations

TypedValue (cn.taketoday.expression.TypedValue)64 Test (org.junit.jupiter.api.Test)34 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)28 ExpressionState (cn.taketoday.expression.spel.ExpressionState)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)14 EvaluationContext (cn.taketoday.expression.EvaluationContext)12 Expression (cn.taketoday.expression.Expression)8 CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)8 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)8 Nullable (cn.taketoday.lang.Nullable)8 MethodParameter (cn.taketoday.core.MethodParameter)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 AccessException (cn.taketoday.expression.AccessException)6 EvaluationException (cn.taketoday.expression.EvaluationException)4 ReflectivePropertyAccessor (cn.taketoday.expression.spel.support.ReflectivePropertyAccessor)4 Time (java.sql.Time)4 Date (java.util.Date)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 ExpressionException (cn.taketoday.expression.ExpressionException)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2