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);
}
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());
}
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());
}
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);
}
Aggregations