Search in sources :

Example 46 with TypedValue

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

the class CompoundExpression method getValueInternal.

/**
 * Evaluates a compound expression. This involves evaluating each piece in turn and the
 * return value from each piece is the active context object for the subsequent piece.
 * @param state the state in which the expression is being evaluated
 * @return the final value from the last piece of the compound expression
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    ValueRef ref = getValueRef(state);
    TypedValue result = ref.getValue();
    this.exitTypeDescriptor = this.children[this.children.length - 1].exitTypeDescriptor;
    return result;
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Example 47 with TypedValue

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

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(org.springframework.expression.TypedValue)

Example 48 with TypedValue

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

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(org.springframework.expression.OperatorOverloader) TypedValue(org.springframework.expression.TypedValue)

Example 49 with TypedValue

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

the class Assign method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue newValue = this.children[1].getValueInternal(state);
    getChild(0).setValue(state, newValue.getValue());
    return newValue;
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Example 50 with TypedValue

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

the class ExpressionStateTests method testTypeConversion.

@Test
public void testTypeConversion() throws EvaluationException {
    ExpressionState state = getState();
    String s = (String) state.convertValue(34, TypeDescriptor.valueOf(String.class));
    assertThat(s).isEqualTo("34");
    s = (String) state.convertValue(new TypedValue(34), TypeDescriptor.valueOf(String.class));
    assertThat(s).isEqualTo("34");
}
Also used : TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

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