use of org.springframework.expression.spel.ExpressionState 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());
}
use of org.springframework.expression.spel.ExpressionState in project spring-framework by spring-projects.
the class OpPlusTests method test_unaryPlusWithStringLiteral.
@Test(expected = SpelEvaluationException.class)
public void test_unaryPlusWithStringLiteral() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
StringLiteral str = new StringLiteral("word", -1, "word");
OpPlus o = new OpPlus(-1, str);
o.getValueInternal(expressionState);
}
use of org.springframework.expression.spel.ExpressionState in project spring-framework by spring-projects.
the class OpPlusTests method test_binaryPlusWithTimeConverted.
@Test
public void test_binaryPlusWithTimeConverted() {
final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new Converter<Time, String>() {
@Override
public String convert(Time source) {
return format.format(source);
}
});
StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
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(format.format(time) + " is now", value.getValue());
}
use of org.springframework.expression.spel.ExpressionState in project spring-framework by spring-projects.
the class OpPlusTests method test_binaryPlusWithStringOperands.
@Test
public void test_binaryPlusWithStringOperands() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
StringLiteral n1 = new StringLiteral("\"foo\"", -1, "\"foo\"");
StringLiteral n2 = new StringLiteral("\"bar\"", -1, "\"bar\"");
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("foobar", value.getValue());
}
use of org.springframework.expression.spel.ExpressionState in project spring-framework by spring-projects.
the class SpelExpression method getValue.
@Override
public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException {
Assert.notNull(context, "EvaluationContext is required");
if (this.compiledAst != null) {
try {
return this.compiledAst.getValue(rootObject, 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, toTypedValue(rootObject), this.configuration);
Object result = this.ast.getValue(expressionState);
checkCompile(expressionState);
return result;
}
Aggregations