Search in sources :

Example 11 with ExpressionState

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

the class SpelExpression method getValueType.

@Override
public Class<?> getValueType(EvaluationContext context, 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(org.springframework.expression.spel.ExpressionState) TypeDescriptor(org.springframework.core.convert.TypeDescriptor)

Example 12 with ExpressionState

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

the class SpelExpression method getValueTypeDescriptor.

@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, 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(org.springframework.expression.spel.ExpressionState)

Example 13 with ExpressionState

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> expectedResultType) throws EvaluationException {
    if (this.compiledAst != null) {
        try {
            Object result = this.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.interpretedCount = 0;
                this.compiledAst = null;
            } 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) TypedValue(org.springframework.expression.TypedValue)

Example 14 with ExpressionState

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
public <T> T getValue(Object rootObject, Class<T> expectedResultType) throws EvaluationException {
    if (this.compiledAst != null) {
        try {
            Object result = this.compiledAst.getValue(rootObject, null);
            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.interpretedCount = 0;
                this.compiledAst = null;
            } 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) TypedValue(org.springframework.expression.TypedValue)

Example 15 with ExpressionState

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

the class SpelExpression method getValue.

// implementing Expression
@Override
public Object getValue() throws EvaluationException {
    Object result;
    if (this.compiledAst != null) {
        try {
            TypedValue contextRoot = evaluationContext == null ? null : evaluationContext.getRootObject();
            return this.compiledAst.getValue(contextRoot == null ? null : contextRoot.getValue(), evaluationContext);
        } catch (Throwable ex) {
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.interpretedCount = 0;
                this.compiledAst = null;
            } 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);
    result = this.ast.getValue(expressionState);
    checkCompile(expressionState);
    return result;
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ExpressionState(org.springframework.expression.spel.ExpressionState) TypedValue(org.springframework.expression.TypedValue)

Aggregations

ExpressionState (org.springframework.expression.spel.ExpressionState)19 TypedValue (org.springframework.expression.TypedValue)11 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)8 Test (org.junit.Test)6 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)6 Time (java.sql.Time)2 Date (java.util.Date)2 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)2 SimpleDateFormat (java.text.SimpleDateFormat)1 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)1 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)1