Search in sources :

Example 1 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-infrastructure by TAKETODAY.

the class OpPlusTests method test_unaryPlusWithStringLiteral.

@Test
public void test_unaryPlusWithStringLiteral() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    StringLiteral str = new StringLiteral("word", -1, -1, "word");
    OpPlus o = new OpPlus(-1, -1, str);
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> o.getValueInternal(expressionState));
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 2 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-infrastructure by TAKETODAY.

the class OpPlusTests method test_binaryPlusWithLeftStringOperand.

@Test
public void test_binaryPlusWithLeftStringOperand() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    StringLiteral n1 = new StringLiteral("\"number is \"", -1, -1, "\"number is \"");
    LongLiteral n2 = new LongLiteral("123", -1, -1, 123);
    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("number is 123");
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 3 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-infrastructure by TAKETODAY.

the class SpelExpression method getValueTypeDescriptor.

@Override
@Nullable
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);
    return this.ast.getValueInternal(expressionState).getTypeDescriptor();
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) Nullable(cn.taketoday.lang.Nullable)

Example 4 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-infrastructure by TAKETODAY.

the class SpelExpression method getValueTypeDescriptor.

@Override
@Nullable
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    ExpressionState expressionState = new ExpressionState(context, this.configuration);
    return this.ast.getValueInternal(expressionState).getTypeDescriptor();
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) Nullable(cn.taketoday.lang.Nullable)

Example 5 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-infrastructure by TAKETODAY.

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            EvaluationContext context = getEvaluationContext();
            Object result = compiledAst.getValue(context.getRootObject().getValue(), context);
            if (expectedResultType == null) {
                return (T) result;
            } else {
                return ExpressionUtils.convertTypedValue(getEvaluationContext(), new TypedValue(result), expectedResultType);
            }
        } 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(getEvaluationContext(), this.configuration);
    TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
    checkCompile(expressionState);
    return ExpressionUtils.convertTypedValue(expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) TypedValue(cn.taketoday.expression.TypedValue) Nullable(cn.taketoday.lang.Nullable)

Aggregations

ExpressionState (cn.taketoday.expression.spel.ExpressionState)38 Nullable (cn.taketoday.lang.Nullable)24 TypedValue (cn.taketoday.expression.TypedValue)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)18 CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)16 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)16 Test (org.junit.jupiter.api.Test)12 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4 EvaluationContext (cn.taketoday.expression.EvaluationContext)4 Time (java.sql.Time)4 Date (java.util.Date)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 StandardTypeConverter (cn.taketoday.expression.spel.support.StandardTypeConverter)2 SimpleDateFormat (java.text.SimpleDateFormat)2