Search in sources :

Example 1 with CompiledExpression

use of cn.taketoday.expression.spel.CompiledExpression 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)

Example 2 with CompiledExpression

use of cn.taketoday.expression.spel.CompiledExpression in project today-infrastructure 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 3 with CompiledExpression

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

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(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 4 with CompiledExpression

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

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

Example 5 with CompiledExpression

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

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(cn.taketoday.expression.spel.SpelEvaluationException) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) EvaluationException(cn.taketoday.expression.EvaluationException) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException)

Aggregations

CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)18 ExpressionState (cn.taketoday.expression.spel.ExpressionState)16 Nullable (cn.taketoday.lang.Nullable)16 TypedValue (cn.taketoday.expression.TypedValue)8 EvaluationContext (cn.taketoday.expression.EvaluationContext)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 EvaluationException (cn.taketoday.expression.EvaluationException)2