Search in sources :

Example 1 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) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            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(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) CompiledExpression(org.springframework.expression.spel.CompiledExpression) Nullable(org.springframework.lang.Nullable)

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

Example 3 with CompiledExpression

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

the class SpelExpression method compileExpression.

/**
 * Perform expression compilation. This will only succeed once exit descriptors for
 * all nodes have been determined. If the compilation fails and has failed more than
 * 100 times the expression is no longer considered suitable for compilation.
 * @return whether this expression has been successfully compiled
 */
public boolean compileExpression() {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        // Previously compiled
        return true;
    }
    if (this.failedAttempts.get() > FAILED_ATTEMPTS_THRESHOLD) {
        // Don't try again
        return false;
    }
    synchronized (this) {
        if (this.compiledAst != null) {
            // Compiled by another thread before this thread got into the sync block
            return true;
        }
        try {
            SpelCompiler compiler = SpelCompiler.getCompiler(this.configuration.getCompilerClassLoader());
            compiledAst = compiler.compile(this.ast);
            if (compiledAst != null) {
                // Successfully compiled
                this.compiledAst = compiledAst;
                return true;
            } else {
                // Failed to compile
                this.failedAttempts.incrementAndGet();
                return false;
            }
        } catch (Exception ex) {
            // Failed to compile
            this.failedAttempts.incrementAndGet();
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.compiledAst = null;
                this.interpretedCount.set(0);
                return false;
            } else {
                // Running in SpelCompilerMode.immediate mode - propagate exception to caller
                throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_COMPILING_EXPRESSION);
            }
        }
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) CompiledExpression(org.springframework.expression.spel.CompiledExpression) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) EvaluationException(org.springframework.expression.EvaluationException)

Example 4 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(@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(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) TypedValue(org.springframework.expression.TypedValue) Nullable(org.springframework.lang.Nullable)

Example 5 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(@Nullable Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException {
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            Object result = compiledAst.getValue(rootObject, getEvaluationContext());
            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(), toTypedValue(rootObject), this.configuration);
    TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
    checkCompile(expressionState);
    return ExpressionUtils.convertTypedValue(expressionState.getEvaluationContext(), 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