Search in sources :

Example 16 with ExpressionState

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

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

the class OpPlusTests method test_binaryPlusWithTime_ToString.

@Test
public void test_binaryPlusWithTime_ToString() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    Time time = new Time(new Date().getTime());
    VariableReference var = new VariableReference("timeVar", -1);
    var.setValue(expressionState, time);
    StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
    OpPlus o = new OpPlus(-1, var, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertEquals(String.class, value.getTypeDescriptor().getObjectType());
    assertEquals(String.class, value.getTypeDescriptor().getType());
    assertEquals(time + " is now", value.getValue());
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 18 with ExpressionState

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

the class OpPlusTests method test_binaryPlusWithRightStringOperand.

@Test
public void test_binaryPlusWithRightStringOperand() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    LongLiteral n1 = new LongLiteral("123", -1, 123);
    StringLiteral n2 = new StringLiteral("\" is a number\"", -1, "\" is a number\"");
    OpPlus o = new OpPlus(-1, n1, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertEquals(String.class, value.getTypeDescriptor().getObjectType());
    assertEquals(String.class, value.getTypeDescriptor().getType());
    assertEquals("123 is a number", value.getValue());
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 19 with ExpressionState

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

the class SpelExpression method setValue.

@Override
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    this.ast.setValue(new ExpressionState(context, this.configuration), value);
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState)

Aggregations

ExpressionState (org.springframework.expression.spel.ExpressionState)19 TypedValue (org.springframework.expression.TypedValue)11 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)8 Test (org.junit.Test)6 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)6 Time (java.sql.Time)2 Date (java.util.Date)2 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)2 SimpleDateFormat (java.text.SimpleDateFormat)1 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)1 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)1