Search in sources :

Example 11 with ExpressionState

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

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(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 12 with ExpressionState

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

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(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) TypedValue(cn.taketoday.expression.TypedValue) Nullable(cn.taketoday.lang.Nullable)

Example 13 with ExpressionState

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

the class SpelExpression method getValueType.

@Override
@Nullable
public Class<?> getValueType(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
    ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);
    TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor();
    return (typeDescriptor != null ? typeDescriptor.getType() : null);
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) TypeDescriptor(cn.taketoday.core.TypeDescriptor) Nullable(cn.taketoday.lang.Nullable)

Example 14 with ExpressionState

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

the class SpelExpression method getValue.

@Override
@Nullable
public Object getValue(@Nullable Object rootObject) throws EvaluationException {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            return compiledAst.getValue(rootObject, getEvaluationContext());
        } 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(), toTypedValue(rootObject), this.configuration);
    Object result = this.ast.getValue(expressionState);
    checkCompile(expressionState);
    return result;
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) Nullable(cn.taketoday.lang.Nullable)

Example 15 with ExpressionState

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

the class SpelExpression method getValue.

@Override
@Nullable
public Object getValue(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            return compiledAst.getValue(rootObject, context);
        } 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, toTypedValue(rootObject), this.configuration);
    Object result = this.ast.getValue(expressionState);
    checkCompile(expressionState);
    return result;
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) 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