Search in sources :

Example 36 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 true if the left operand is an instanceof of the right operand, otherwise
	 *         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 = null;
    if (rightValue == null || !(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)

Example 37 with TypedValue

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

the class Projection method getValueRef.

@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    TypedValue op = state.getActiveContextObject();
    Object operand = op.getValue();
    boolean operandIsArray = ObjectUtils.isArray(operand);
    // eg. {'a':'y','b':'n'}.![value=='y'?key:null]" == ['a', null]
    if (operand instanceof Map) {
        Map<?, ?> mapData = (Map<?, ?>) operand;
        List<Object> result = new ArrayList<>();
        for (Map.Entry<?, ?> entry : mapData.entrySet()) {
            try {
                state.pushActiveContextObject(new TypedValue(entry));
                state.enterScope();
                result.add(this.children[0].getValueInternal(state).getValue());
            } finally {
                state.popActiveContextObject();
                state.exitScope();
            }
        }
        // TODO unable to build correct type descriptor
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
    }
    if (operand instanceof Iterable || operandIsArray) {
        Iterable<?> data = (operand instanceof Iterable ? (Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
        List<Object> result = new ArrayList<>();
        int idx = 0;
        Class<?> arrayElementType = null;
        for (Object element : data) {
            try {
                state.pushActiveContextObject(new TypedValue(element));
                state.enterScope("index", idx);
                Object value = this.children[0].getValueInternal(state).getValue();
                if (value != null && operandIsArray) {
                    arrayElementType = determineCommonType(arrayElementType, value.getClass());
                }
                result.add(value);
            } finally {
                state.exitScope();
                state.popActiveContextObject();
            }
            idx++;
        }
        if (operandIsArray) {
            if (arrayElementType == null) {
                arrayElementType = Object.class;
            }
            Object resultArray = Array.newInstance(arrayElementType, result.size());
            System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
            return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray), this);
        }
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
    }
    if (operand == null) {
        if (this.nullSafe) {
            return ValueRef.NullValueRef.INSTANCE;
        }
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
    }
    throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClass().getName());
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ArrayList(java.util.ArrayList) Map(java.util.Map) TypedValue(org.springframework.expression.TypedValue)

Example 38 with TypedValue

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> expectedResultType) throws EvaluationException {
    if (this.compiledAst != null) {
        try {
            Object result = this.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.interpretedCount = 0;
                this.compiledAst = null;
            } 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) TypedValue(org.springframework.expression.TypedValue)

Example 39 with TypedValue

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
public <T> T getValue(Object rootObject, Class<T> expectedResultType) throws EvaluationException {
    if (this.compiledAst != null) {
        try {
            Object result = this.compiledAst.getValue(rootObject, null);
            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.interpretedCount = 0;
                this.compiledAst = null;
            } 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) TypedValue(org.springframework.expression.TypedValue)

Example 40 with TypedValue

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

the class SpelExpression method getValue.

// implementing Expression
@Override
public Object getValue() throws EvaluationException {
    Object result;
    if (this.compiledAst != null) {
        try {
            TypedValue contextRoot = evaluationContext == null ? null : evaluationContext.getRootObject();
            return this.compiledAst.getValue(contextRoot == null ? null : contextRoot.getValue(), evaluationContext);
        } catch (Throwable ex) {
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.interpretedCount = 0;
                this.compiledAst = null;
            } 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);
    result = this.ast.getValue(expressionState);
    checkCompile(expressionState);
    return result;
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ExpressionState(org.springframework.expression.spel.ExpressionState) TypedValue(org.springframework.expression.TypedValue)

Aggregations

TypedValue (org.springframework.expression.TypedValue)64 Test (org.junit.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)18 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)13 ExpressionState (org.springframework.expression.spel.ExpressionState)11 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 AccessException (org.springframework.expression.AccessException)4 Expression (org.springframework.expression.Expression)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 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2