use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class SpelExpression method getValue.
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException {
Assert.notNull(context, "EvaluationContext is required");
CompiledExpression compiledAst = this.compiledAst;
if (compiledAst != null) {
try {
Object result = compiledAst.getValue(context.getRootObject().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.compiledAst = null;
this.interpretedCount.set(0);
} 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.TypedValue in project spring-framework by spring-projects.
the class SpelReproTests method SPR9994_bridgeMethods.
@Test
void SPR9994_bridgeMethods() throws Exception {
ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
StandardEvaluationContext context = new StandardEvaluationContext();
GenericImplementation target = new GenericImplementation();
accessor.write(context, target, "property", "1");
assertThat(target.value).isEqualTo(1);
TypedValue value = accessor.read(context, target, "property");
assertThat(value.getValue()).isEqualTo(1);
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
assertThat(value.getTypeDescriptor().getAnnotations()).isNotEmpty();
}
use of org.springframework.expression.TypedValue 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, -1, 123);
StringLiteral n2 = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\"");
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
assertThat(value.getValue()).isEqualTo("123 is a number");
}
use of org.springframework.expression.TypedValue 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, -1);
var.setValue(expressionState, time);
StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
OpPlus o = new OpPlus(-1, -1, var, n2);
TypedValue value = o.getValueInternal(expressionState);
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
assertThat(value.getValue()).isEqualTo((time + " is now"));
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class ReflectionHelperTests method testTypedValue.
@Test
public void testTypedValue() {
TypedValue tv1 = new TypedValue("hello");
TypedValue tv2 = new TypedValue("hello");
TypedValue tv3 = new TypedValue("bye");
assertThat(tv1.getTypeDescriptor().getType()).isEqualTo(String.class);
assertThat(tv1.toString()).isEqualTo("TypedValue: 'hello' of [java.lang.String]");
assertThat(tv2).isEqualTo(tv1);
assertThat(tv1).isEqualTo(tv2);
assertThat(tv3).isNotEqualTo(tv1);
assertThat(tv3).isNotEqualTo(tv2);
assertThat(tv1).isNotEqualTo(tv3);
assertThat(tv2).isNotEqualTo(tv3);
assertThat(tv2.hashCode()).isEqualTo(tv1.hashCode());
assertThat(tv3.hashCode()).isNotEqualTo(tv1.hashCode());
assertThat(tv3.hashCode()).isNotEqualTo(tv2.hashCode());
}
Aggregations