Search in sources :

Example 61 with TypedValue

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

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

the class FunctionReference method executeFunctionJLRMethod.

/**
 * Execute a function represented as a {@code java.lang.reflect.Method}.
 *
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
    Object[] functionArgs = getArguments(state);
    if (!method.isVarArgs()) {
        int declaredParamCount = method.getParameterCount();
        if (declaredParamCount != functionArgs.length) {
            throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, functionArgs.length, declaredParamCount);
        }
    }
    if (!Modifier.isStatic(method.getModifiers())) {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
    }
    // Convert arguments if necessary and remap them for varargs if required
    TypeConverter converter = state.getEvaluationContext().getTypeConverter();
    boolean argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
    if (method.isVarArgs()) {
        functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
    }
    boolean compilable = false;
    try {
        ReflectionUtils.makeAccessible(method);
        Object result = method.invoke(method.getClass(), functionArgs);
        compilable = !argumentConversionOccurred;
        return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
    } catch (Exception ex) {
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, this.name, ex.getMessage());
    } finally {
        if (compilable) {
            this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
            this.method = method;
        } else {
            this.exitTypeDescriptor = null;
            this.method = null;
        }
    }
}
Also used : TypeConverter(cn.taketoday.expression.TypeConverter) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) EvaluationException(cn.taketoday.expression.EvaluationException) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) TypedValue(cn.taketoday.expression.TypedValue)

Example 63 with TypedValue

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

the class Elvis method getValueInternal.

/**
 * Evaluate the condition and if not null, return it.
 * If it is null, return the other value.
 *
 * @param state the expression state
 * @throws EvaluationException if the condition does not evaluate correctly
 * to a boolean or there is a problem executing the chosen alternative
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue value = this.children[0].getValueInternal(state);
    // If this check is changed, the generateCode method will need changing too
    if (value.getValue() != null && !"".equals(value.getValue())) {
        return value;
    } else {
        TypedValue result = this.children[1].getValueInternal(state);
        computeExitTypeDescriptor();
        return result;
    }
}
Also used : TypedValue(cn.taketoday.expression.TypedValue)

Example 64 with TypedValue

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

the class ExpressionState method operate.

public TypedValue operate(Operation op, @Nullable Object left, @Nullable Object right) throws EvaluationException {
    OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
    if (overloader.overridesOperation(op, left, right)) {
        Object returnValue = overloader.operate(op, left, right);
        return new TypedValue(returnValue);
    } else {
        String leftType = (left == null ? "null" : left.getClass().getName());
        String rightType = (right == null ? "null" : right.getClass().getName());
        throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
    }
}
Also used : OperatorOverloader(cn.taketoday.expression.OperatorOverloader) TypedValue(cn.taketoday.expression.TypedValue)

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