Search in sources :

Example 21 with TypedValue

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

Example 22 with TypedValue

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

Example 23 with TypedValue

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

Example 24 with TypedValue

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]);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 25 with TypedValue

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

Aggregations

TypedValue (org.springframework.expression.TypedValue)63 Test (org.junit.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)18 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)13 ExpressionState (org.springframework.expression.spel.ExpressionState)11 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 AccessException (org.springframework.expression.AccessException)4 Expression (org.springframework.expression.Expression)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 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2