Search in sources :

Example 61 with TypedValue

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);
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ExpressionState(org.springframework.expression.spel.ExpressionState) CompiledExpression(org.springframework.expression.spel.CompiledExpression) TypedValue(org.springframework.expression.TypedValue) Nullable(org.springframework.lang.Nullable)

Example 62 with TypedValue

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();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 63 with TypedValue

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");
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 64 with TypedValue

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"));
}
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.jupiter.api.Test)

Example 65 with TypedValue

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());
}
Also used : TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Aggregations

TypedValue (org.springframework.expression.TypedValue)65 Test (org.junit.jupiter.api.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)16 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)14 ExpressionState (org.springframework.expression.spel.ExpressionState)9 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 AccessException (org.springframework.expression.AccessException)5 Expression (org.springframework.expression.Expression)4 CompiledExpression (org.springframework.expression.spel.CompiledExpression)4 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)4 MethodParameter (org.springframework.core.MethodParameter)3 EvaluationException (org.springframework.expression.EvaluationException)3 SpelNode (org.springframework.expression.spel.SpelNode)3 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)3 Nullable (org.springframework.lang.Nullable)3