Search in sources :

Example 41 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, Class<T> expectedResultType) throws EvaluationException {
    if (this.compiledAst != null) {
        try {
            TypedValue contextRoot = context == null ? null : context.getRootObject();
            Object result = this.compiledAst.getValue(contextRoot == null ? null : contextRoot.getValue(), 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, 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 42 with TypedValue

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

the class OpModulus method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
    Object rightOperand = getRightOperand().getValueInternal(state).getValue();
    if (leftOperand instanceof Number && rightOperand instanceof Number) {
        Number leftNumber = (Number) leftOperand;
        Number rightNumber = (Number) rightOperand;
        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
        } else if (leftNumber instanceof Double || rightNumber instanceof Double) {
            this.exitTypeDescriptor = "D";
            return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
        } else if (leftNumber instanceof Float || rightNumber instanceof Float) {
            this.exitTypeDescriptor = "F";
            return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
        } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
            BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
            BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
            return new TypedValue(leftBigInteger.remainder(rightBigInteger));
        } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
            this.exitTypeDescriptor = "J";
            return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
        } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
            this.exitTypeDescriptor = "I";
            return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
        } else {
            // Unknown Number subtypes -> best guess is double division
            return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
        }
    }
    return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) TypedValue(org.springframework.expression.TypedValue)

Example 43 with TypedValue

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

the class Indexer method getValueRef.

@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    TypedValue context = state.getActiveContextObject();
    Object targetObject = context.getValue();
    TypeDescriptor targetDescriptor = context.getTypeDescriptor();
    TypedValue indexValue = null;
    Object index = null;
    // the property (SPR-5847)
    if (targetObject instanceof Map && (this.children[0] instanceof PropertyOrFieldReference)) {
        PropertyOrFieldReference reference = (PropertyOrFieldReference) this.children[0];
        index = reference.getName();
        indexValue = new TypedValue(index);
    } else {
        // the root object so temporarily push that on whilst evaluating the key
        try {
            state.pushActiveContextObject(state.getRootContextObject());
            indexValue = this.children[0].getValueInternal(state);
            index = indexValue.getValue();
        } finally {
            state.popActiveContextObject();
        }
    }
    // Indexing into a Map
    if (targetObject instanceof Map) {
        Object key = index;
        if (targetDescriptor.getMapKeyTypeDescriptor() != null) {
            key = state.convertValue(key, targetDescriptor.getMapKeyTypeDescriptor());
        }
        this.indexedType = IndexedType.MAP;
        return new MapIndexingValueRef(state.getTypeConverter(), (Map<?, ?>) targetObject, key, targetDescriptor);
    }
    if (targetObject == null) {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
    }
    // attempt to treat the index value as a number
    if (targetObject.getClass().isArray() || targetObject instanceof Collection || targetObject instanceof String) {
        int idx = (Integer) state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
        if (targetObject.getClass().isArray()) {
            this.indexedType = IndexedType.ARRAY;
            return new ArrayIndexingValueRef(state.getTypeConverter(), targetObject, idx, targetDescriptor);
        } else if (targetObject instanceof Collection) {
            if (targetObject instanceof List) {
                this.indexedType = IndexedType.LIST;
            }
            return new CollectionIndexingValueRef((Collection<?>) targetObject, idx, targetDescriptor, state.getTypeConverter(), state.getConfiguration().isAutoGrowCollections(), state.getConfiguration().getMaximumAutoGrowSize());
        } else {
            this.indexedType = IndexedType.STRING;
            return new StringIndexingLValue((String) targetObject, idx, targetDescriptor);
        }
    }
    // TODO could call the conversion service to convert the value to a String
    if (String.class == indexValue.getTypeDescriptor().getType()) {
        this.indexedType = IndexedType.OBJECT;
        return new PropertyIndexingValueRef(targetObject, (String) indexValue.getValue(), state.getEvaluationContext(), targetDescriptor);
    }
    throw new SpelEvaluationException(getStartPosition(), SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetDescriptor.toString());
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map) TypedValue(org.springframework.expression.TypedValue)

Example 44 with TypedValue

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

the class InlineMap method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException {
    if (this.constant != null) {
        return this.constant;
    } else {
        Map<Object, Object> returnValue = new LinkedHashMap<>();
        int childcount = getChildCount();
        for (int c = 0; c < childcount; c++) {
            // TODO allow for key being PropertyOrFieldReference like Indexer on maps
            SpelNode keyChild = getChild(c++);
            Object key = null;
            if (keyChild instanceof PropertyOrFieldReference) {
                PropertyOrFieldReference reference = (PropertyOrFieldReference) keyChild;
                key = reference.getName();
            } else {
                key = keyChild.getValue(expressionState);
            }
            Object value = getChild(c).getValue(expressionState);
            returnValue.put(key, value);
        }
        return new TypedValue(returnValue);
    }
}
Also used : SpelNode(org.springframework.expression.spel.SpelNode) LinkedHashMap(java.util.LinkedHashMap) TypedValue(org.springframework.expression.TypedValue)

Example 45 with TypedValue

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

the class MethodReference method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    EvaluationContext evaluationContext = state.getEvaluationContext();
    Object value = state.getActiveContextObject().getValue();
    TypeDescriptor targetType = state.getActiveContextObject().getTypeDescriptor();
    Object[] arguments = getArguments(state);
    TypedValue result = getValueInternal(evaluationContext, value, targetType, arguments);
    updateExitTypeDescriptor();
    return result;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) EvaluationContext(org.springframework.expression.EvaluationContext) 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