Search in sources :

Example 6 with CompiledExpression

use of org.springframework.expression.spel.CompiledExpression in project spring-framework by spring-projects.

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            Object result = compiledAst.getValue(rootObject, 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, toTypedValue(rootObject), 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 7 with CompiledExpression

use of org.springframework.expression.spel.CompiledExpression in project spring-framework by spring-projects.

the class SpelExpression method getValue.

@Override
@Nullable
public Object getValue() throws EvaluationException {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            EvaluationContext context = getEvaluationContext();
            return compiledAst.getValue(context.getRootObject().getValue(), 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(getEvaluationContext(), 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) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) CompiledExpression(org.springframework.expression.spel.CompiledExpression) Nullable(org.springframework.lang.Nullable)

Example 8 with CompiledExpression

use of org.springframework.expression.spel.CompiledExpression in project spring-framework by spring-projects.

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(org.springframework.expression.spel.SpelEvaluationException) ExpressionState(org.springframework.expression.spel.ExpressionState) CompiledExpression(org.springframework.expression.spel.CompiledExpression) Nullable(org.springframework.lang.Nullable)

Example 9 with CompiledExpression

use of org.springframework.expression.spel.CompiledExpression 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)

Aggregations

CompiledExpression (org.springframework.expression.spel.CompiledExpression)9 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)9 ExpressionState (org.springframework.expression.spel.ExpressionState)8 Nullable (org.springframework.lang.Nullable)8 TypedValue (org.springframework.expression.TypedValue)4 EvaluationContext (org.springframework.expression.EvaluationContext)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 EvaluationException (org.springframework.expression.EvaluationException)1