Search in sources :

Example 56 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class VariableReference method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState state) throws SpelEvaluationException {
    if (this.name.equals(THIS)) {
        return state.getActiveContextObject();
    }
    if (this.name.equals(ROOT)) {
        TypedValue result = state.getRootContextObject();
        this.exitTypeDescriptor = CodeFlow.toDescriptorFromObject(result.getValue());
        return result;
    }
    TypedValue result = state.lookupVariable(this.name);
    Object value = result.getValue();
    if (value == null || !Modifier.isPublic(value.getClass().getModifiers())) {
        // If the type is not public then when generateCode produces a checkcast to it
        // then an IllegalAccessError will occur.
        // If resorting to Object isn't sufficient, the hierarchy could be traversed for
        // the first public type.
        this.exitTypeDescriptor = "Ljava/lang/Object";
    } else {
        this.exitTypeDescriptor = CodeFlow.toDescriptorFromObject(value);
    }
    // a null value will mean either the value was null or the variable was not found
    return result;
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Example 57 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

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(org.springframework.expression.spel.SpelEvaluationException) ExpressionState(org.springframework.expression.spel.ExpressionState) CompiledExpression(org.springframework.expression.spel.CompiledExpression) TypedValue(org.springframework.expression.TypedValue) Nullable(org.springframework.lang.Nullable)

Example 58 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

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(org.springframework.expression.spel.SpelEvaluationException) ExpressionState(org.springframework.expression.spel.ExpressionState) CompiledExpression(org.springframework.expression.spel.CompiledExpression) TypedValue(org.springframework.expression.TypedValue) Nullable(org.springframework.lang.Nullable)

Example 59 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

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(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) MethodParameter(org.springframework.core.MethodParameter) AccessException(org.springframework.expression.AccessException) TypedValue(org.springframework.expression.TypedValue)

Example 60 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class OperatorInstanceof method getValueInternal.

/**
 * Compare the left operand to see it is an instance of the type specified as the
 * right operand. The right operand must be a class.
 * @param state the expression state
 * @return {@code true} if the left operand is an instanceof of the right operand,
 * otherwise {@code false}
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    SpelNodeImpl rightOperand = getRightOperand();
    TypedValue left = getLeftOperand().getValueInternal(state);
    TypedValue right = rightOperand.getValueInternal(state);
    Object leftValue = left.getValue();
    Object rightValue = right.getValue();
    BooleanTypedValue result;
    if (!(rightValue instanceof Class)) {
        throw new SpelEvaluationException(getRightOperand().getStartPosition(), SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND, (rightValue == null ? "null" : rightValue.getClass().getName()));
    }
    Class<?> rightClass = (Class<?>) rightValue;
    if (leftValue == null) {
        // null is not an instanceof anything
        result = BooleanTypedValue.FALSE;
    } else {
        result = BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
    }
    this.type = rightClass;
    if (rightOperand instanceof TypeReference) {
        // Can only generate bytecode where the right operand is a direct type reference,
        // not if it is indirect (for example when right operand is a variable reference)
        this.exitTypeDescriptor = "Z";
    }
    return result;
}
Also used : BooleanTypedValue(org.springframework.expression.spel.support.BooleanTypedValue) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) BooleanTypedValue(org.springframework.expression.spel.support.BooleanTypedValue) TypedValue(org.springframework.expression.TypedValue)

Aggregations

TypedValue (org.springframework.expression.TypedValue)65 Test (org.junit.jupiter.api.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)16 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)14 ExpressionState (org.springframework.expression.spel.ExpressionState)9 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 AccessException (org.springframework.expression.AccessException)5 Expression (org.springframework.expression.Expression)4 CompiledExpression (org.springframework.expression.spel.CompiledExpression)4 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)4 MethodParameter (org.springframework.core.MethodParameter)3 EvaluationException (org.springframework.expression.EvaluationException)3 SpelNode (org.springframework.expression.spel.SpelNode)3 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)3 Nullable (org.springframework.lang.Nullable)3