Search in sources :

Example 16 with SpelEvaluationException

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

the class SpelExpression method getValue.

@Override
public Object getValue(EvaluationContext context) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    if (compiledAst != null) {
        try {
            TypedValue contextRoot = context == null ? null : context.getRootObject();
            return this.compiledAst.getValue(contextRoot != null ? contextRoot.getValue() : null, context);
        } 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);
    Object 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)

Example 17 with SpelEvaluationException

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

the class SpelExpression method getValue.

@Override
public Object getValue(Object rootObject) throws EvaluationException {
    Object result;
    if (this.compiledAst != null) {
        try {
            return this.compiledAst.getValue(rootObject, 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(), toTypedValue(rootObject), 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)

Example 18 with SpelEvaluationException

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

the class OperatorBetween method getValueInternal.

/**
	 * Returns a boolean based on whether a value is in the range expressed. The first
	 * operand is any value whilst the second is a list of two values - those two values
	 * being the bounds allowed for the first operand (inclusive).
	 * @param state the expression state
	 * @return true if the left operand is in the range specified, false otherwise
	 * @throws EvaluationException if there is a problem evaluating the expression
	 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();
    if (!(right instanceof List) || ((List<?>) right).size() != 2) {
        throw new SpelEvaluationException(getRightOperand().getStartPosition(), SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
    }
    List<?> list = (List<?>) right;
    Object low = list.get(0);
    Object high = list.get(1);
    TypeComparator comp = state.getTypeComparator();
    try {
        return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
    } catch (SpelEvaluationException ex) {
        ex.setPosition(getStartPosition());
        throw ex;
    }
}
Also used : TypeComparator(org.springframework.expression.TypeComparator) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) List(java.util.List)

Example 19 with SpelEvaluationException

use of org.springframework.expression.spel.SpelEvaluationException 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 20 with SpelEvaluationException

use of org.springframework.expression.spel.SpelEvaluationException 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)

Aggregations

SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)30 TypedValue (org.springframework.expression.TypedValue)18 ExpressionState (org.springframework.expression.spel.ExpressionState)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)6 AccessException (org.springframework.expression.AccessException)6 ArrayList (java.util.ArrayList)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Map (java.util.Map)4 EvaluationException (org.springframework.expression.EvaluationException)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 TypeConverter (org.springframework.expression.TypeConverter)3 HashMap (java.util.HashMap)2 List (java.util.List)2 MethodParameter (org.springframework.core.MethodParameter)2 ConstructorExecutor (org.springframework.expression.ConstructorExecutor)2 EvaluationContext (org.springframework.expression.EvaluationContext)2 PropertyAccessor (org.springframework.expression.PropertyAccessor)2 CompilablePropertyAccessor (org.springframework.expression.spel.CompilablePropertyAccessor)2 ReflectiveConstructorExecutor (org.springframework.expression.spel.support.ReflectiveConstructorExecutor)2