use of org.springframework.expression.TypedValue 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;
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class ExpressionStateTests method testVariables.
@Test
public void testVariables() {
ExpressionState state = getState();
TypedValue typedValue = state.lookupVariable("foo");
assertEquals(TypedValue.NULL, typedValue);
state.setVariable("foo", 34);
typedValue = state.lookupVariable("foo");
assertEquals(34, typedValue.getValue());
assertEquals(Integer.class, typedValue.getTypeDescriptor().getType());
state.setVariable("foo", "abc");
typedValue = state.lookupVariable("foo");
assertEquals("abc", typedValue.getValue());
assertEquals(String.class, typedValue.getTypeDescriptor().getType());
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class ExpressionStateTests method testRootObjectConstructor.
@Test
public void testRootObjectConstructor() {
EvaluationContext ctx = getContext();
// TypedValue root = ctx.getRootObject();
// supplied should override root on context
ExpressionState state = new ExpressionState(ctx, new TypedValue("i am a string"));
TypedValue stateRoot = state.getRootContextObject();
assertEquals(String.class, stateRoot.getTypeDescriptor().getType());
assertEquals("i am a string", stateRoot.getValue());
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method projectionWithArray.
@Test
public void projectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testArray", IntegerTestBean.createArray());
Object value = expression.getValue(context);
assertTrue(value.getClass().isArray());
TypedValue typedValue = new TypedValue(value);
assertEquals(Number.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
Number[] array = (Number[]) value;
assertEquals(3, array.length);
assertEquals(new Integer(5), array[0]);
assertEquals(5.9f, array[1]);
assertEquals(new Integer(7), array[2]);
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class OpPlusTests method test_binaryPlusWithLeftStringOperand.
@Test
public void test_binaryPlusWithLeftStringOperand() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
StringLiteral n1 = new StringLiteral("\"number is \"", -1, "\"number is \"");
LongLiteral n2 = new LongLiteral("123", -1, 123);
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("number is 123", value.getValue());
}
Aggregations