Search in sources :

Example 6 with ExpressionState

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

Example 7 with ExpressionState

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

Example 8 with 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());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) ExpressionState(org.springframework.expression.spel.ExpressionState) SimpleDateFormat(java.text.SimpleDateFormat) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 9 with ExpressionState

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

Example 10 with ExpressionState

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

Aggregations

ExpressionState (org.springframework.expression.spel.ExpressionState)20 TypedValue (org.springframework.expression.TypedValue)11 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)8 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)7 Test (org.junit.Test)6 Time (java.sql.Time)2 Date (java.util.Date)2 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)2 SimpleDateFormat (java.text.SimpleDateFormat)1 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)1 ExpressionNode (org.springframework.data.mongodb.core.spel.ExpressionNode)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)1