Class SpelExpression
- All Implemented Interfaces:
Expression
SpelExpression
represents a parsed (valid) expression that is ready to be
evaluated in a specified context. An expression can be evaluated standalone or in a
specified context. During expression evaluation the context may be asked to resolve
references to types, beans, properties, and methods.- Since:
- 3.0
- Author:
- Andy Clement, Juergen Hoeller, Sam Brannen
-
Constructor Summary
ConstructorDescriptionSpelExpression
(String expression, SpelNodeImpl ast, SpelParserConfiguration configuration) Construct an expression, only used by the parser. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Perform expression compilation.getAST()
Return the Abstract Syntax Tree for the expression.Return the default evaluation context that will be used if none is supplied on an evaluation call.Return the original string used to create this expression (unmodified).getValue()
Evaluate this expression in the default context and return the result of evaluation.<T> T
Evaluate this expression in the default context and return the result of evaluation.Evaluate this expression in the default context against the specified root object and return the result of evaluation.<T> T
Evaluate this expression in the default context against the specified root object and return the result of evaluation.getValue
(EvaluationContext context) Evaluate this expression in the provided context and return the result of evaluation.<T> T
getValue
(EvaluationContext context, Class<T> expectedResultType) Evaluate this expression in the provided context and return the result of evaluation.getValue
(EvaluationContext context, Object rootObject) Evaluate this expression in the provided context against the specified root object and return the result of evaluation.<T> T
getValue
(EvaluationContext context, Object rootObject, Class<T> expectedResultType) Evaluate this expression in the provided context against the specified root object and return the result of evaluation.Class<?>
Return the most general type that can be passed to theExpression.setValue(EvaluationContext, Object)
method using the default context.Class<?>
getValueType
(Object rootObject) Return the most general type that can be passed to theExpression.setValue(Object, Object)
method using the default context.Class<?>
getValueType
(EvaluationContext context) Return the most general type that can be passed to theExpression.setValue(EvaluationContext, Object)
method for the given context.Class<?>
getValueType
(EvaluationContext context, Object rootObject) Return the most general type that can be passed to theExpression.setValue(EvaluationContext, Object, Object)
method for the given context.Return a descriptor for the most general type that can be passed to one of thesetValue(...)
methods using the default context.getValueTypeDescriptor
(Object rootObject) Return a descriptor for the most general type that can be passed to theExpression.setValue(Object, Object)
method using the default context.getValueTypeDescriptor
(EvaluationContext context) Return a descriptor for the most general type that can be passed to theExpression.setValue(EvaluationContext, Object)
method for the given context.getValueTypeDescriptor
(EvaluationContext context, Object rootObject) Return a descriptor for the most general type that can be passed to theExpression.setValue(EvaluationContext, Object, Object)
method for the given context.boolean
isWritable
(Object rootObject) Determine if this expression can be written to, i.e.boolean
isWritable
(EvaluationContext context) Determine if this expression can be written to, i.e.boolean
isWritable
(EvaluationContext context, Object rootObject) Determine if this expression can be written to, i.e.void
Cause an expression to revert to being interpreted if it has been using a compiled form.void
setEvaluationContext
(EvaluationContext evaluationContext) Set the evaluation context that will be used if none is specified on an evaluation call.void
Set this expression in the default context to the value provided.void
setValue
(EvaluationContext context, Object value) Set this expression in the provided context to the value provided.void
setValue
(EvaluationContext context, Object rootObject, Object value) Set this expression in the provided context to the value provided.Produce a string representation of the Abstract Syntax Tree for the expression.
-
Constructor Details
-
SpelExpression
Construct an expression, only used by the parser.
-
-
Method Details
-
setEvaluationContext
Set the evaluation context that will be used if none is specified on an evaluation call.- Parameters:
evaluationContext
- the evaluation context to use
-
getEvaluationContext
Return the default evaluation context that will be used if none is supplied on an evaluation call.- Returns:
- the default evaluation context
-
getExpressionString
Description copied from interface:Expression
Return the original string used to create this expression (unmodified).- Specified by:
getExpressionString
in interfaceExpression
- Returns:
- the original expression string
-
getValue
Description copied from interface:Expression
Evaluate this expression in the default context and return the result of evaluation.- Specified by:
getValue
in interfaceExpression
- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
Description copied from interface:Expression
Evaluate this expression in the default context and return the result of evaluation.If the result of the evaluation does not match (and cannot be converted to) the expected result type then an exception will be thrown.
- Specified by:
getValue
in interfaceExpression
- Parameters:
expectedResultType
- the type the caller would like the result to be- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
Description copied from interface:Expression
Evaluate this expression in the default context against the specified root object and return the result of evaluation.- Specified by:
getValue
in interfaceExpression
- Parameters:
rootObject
- the root object against which to evaluate the expression- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
@Nullable public <T> T getValue(@Nullable Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException Description copied from interface:Expression
Evaluate this expression in the default context against the specified root object and return the result of evaluation.If the result of the evaluation does not match (and cannot be converted to) the expected result type then an exception will be thrown.
- Specified by:
getValue
in interfaceExpression
- Parameters:
rootObject
- the root object against which to evaluate the expressionexpectedResultType
- the type the caller would like the result to be- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
Description copied from interface:Expression
Evaluate this expression in the provided context and return the result of evaluation.- Specified by:
getValue
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expression- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
@Nullable public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException Description copied from interface:Expression
Evaluate this expression in the provided context and return the result of evaluation.If the result of the evaluation does not match (and cannot be converted to) the expected result type then an exception will be thrown.
- Specified by:
getValue
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expressionexpectedResultType
- the type the caller would like the result to be- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
@Nullable public Object getValue(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException Description copied from interface:Expression
Evaluate this expression in the provided context against the specified root object and return the result of evaluation.The supplied root object will be used as an override for any default root object configured in the context.
- Specified by:
getValue
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expressionrootObject
- the root object against which to evaluate the expression- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValue
@Nullable public <T> T getValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException Description copied from interface:Expression
Evaluate this expression in the provided context against the specified root object and return the result of evaluation.The supplied root object will be used as an override for any default root object configured in the context.
If the result of the evaluation does not match (and cannot be converted to) the expected result type then an exception will be thrown.
- Specified by:
getValue
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expressionrootObject
- the root object against which to evaluate the expressionexpectedResultType
- the type the caller would like the result to be- Returns:
- the evaluation result
- Throws:
EvaluationException
- if there is a problem during evaluation
-
getValueType
Description copied from interface:Expression
Return the most general type that can be passed to theExpression.setValue(EvaluationContext, Object)
method using the default context.- Specified by:
getValueType
in interfaceExpression
- Returns:
- the most general type of value that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueType
Description copied from interface:Expression
Return the most general type that can be passed to theExpression.setValue(Object, Object)
method using the default context.- Specified by:
getValueType
in interfaceExpression
- Parameters:
rootObject
- the root object against which to evaluate the expression- Returns:
- the most general type of value that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueType
Description copied from interface:Expression
Return the most general type that can be passed to theExpression.setValue(EvaluationContext, Object)
method for the given context.- Specified by:
getValueType
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expression- Returns:
- the most general type of value that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueType
@Nullable public Class<?> getValueType(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException Description copied from interface:Expression
Return the most general type that can be passed to theExpression.setValue(EvaluationContext, Object, Object)
method for the given context.The supplied root object will be used as an override for any default root object configured in the context.
- Specified by:
getValueType
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expressionrootObject
- the root object against which to evaluate the expression- Returns:
- the most general type of value that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueTypeDescriptor
Description copied from interface:Expression
Return a descriptor for the most general type that can be passed to one of thesetValue(...)
methods using the default context.- Specified by:
getValueTypeDescriptor
in interfaceExpression
- Returns:
- a type descriptor for values that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueTypeDescriptor
@Nullable public TypeDescriptor getValueTypeDescriptor(@Nullable Object rootObject) throws EvaluationException Description copied from interface:Expression
Return a descriptor for the most general type that can be passed to theExpression.setValue(Object, Object)
method using the default context.- Specified by:
getValueTypeDescriptor
in interfaceExpression
- Parameters:
rootObject
- the root object against which to evaluate the expression- Returns:
- a type descriptor for values that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueTypeDescriptor
@Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException Description copied from interface:Expression
Return a descriptor for the most general type that can be passed to theExpression.setValue(EvaluationContext, Object)
method for the given context.- Specified by:
getValueTypeDescriptor
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expression- Returns:
- a type descriptor for values that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
getValueTypeDescriptor
@Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException Description copied from interface:Expression
Return a descriptor for the most general type that can be passed to theExpression.setValue(EvaluationContext, Object, Object)
method for the given context.The supplied root object will be used as an override for any default root object configured in the context.
- Specified by:
getValueTypeDescriptor
in interfaceExpression
- Parameters:
context
- the context in which to evaluate the expressionrootObject
- the root object against which to evaluate the expression- Returns:
- a type descriptor for values that can be set in this context
- Throws:
EvaluationException
- if there is a problem determining the type
-
isWritable
Description copied from interface:Expression
Determine if this expression can be written to, i.e. setValue() can be called.- Specified by:
isWritable
in interfaceExpression
- Parameters:
rootObject
- the root object against which to evaluate the expression- Returns:
true
if the expression is writable;false
otherwise- Throws:
EvaluationException
- if there is a problem determining if it is writable
-
isWritable
Description copied from interface:Expression
Determine if this expression can be written to, i.e. setValue() can be called.- Specified by:
isWritable
in interfaceExpression
- Parameters:
context
- the context in which the expression should be checked- Returns:
true
if the expression is writable;false
otherwise- Throws:
EvaluationException
- if there is a problem determining if it is writable
-
isWritable
public boolean isWritable(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException Description copied from interface:Expression
Determine if this expression can be written to, i.e. setValue() can be called.The supplied root object will be used as an override for any default root object configured in the context.
- Specified by:
isWritable
in interfaceExpression
- Parameters:
context
- the context in which the expression should be checkedrootObject
- the root object against which to evaluate the expression- Returns:
true
if the expression is writable;false
otherwise- Throws:
EvaluationException
- if there is a problem determining if it is writable
-
setValue
public void setValue(@Nullable Object rootObject, @Nullable Object value) throws EvaluationException Description copied from interface:Expression
Set this expression in the default context to the value provided.- Specified by:
setValue
in interfaceExpression
- Parameters:
rootObject
- the root object against which to evaluate the expressionvalue
- the new value- Throws:
EvaluationException
- if there is a problem during evaluation
-
setValue
Description copied from interface:Expression
Set this expression in the provided context to the value provided.- Specified by:
setValue
in interfaceExpression
- Parameters:
context
- the context in which to set the value of the expressionvalue
- the new value- Throws:
EvaluationException
- if there is a problem during evaluation
-
setValue
public void setValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Object value) throws EvaluationException Description copied from interface:Expression
Set this expression in the provided context to the value provided.The supplied root object will be used as an override for any default root object configured in the context.
- Specified by:
setValue
in interfaceExpression
- Parameters:
context
- the context in which to set the value of the expressionrootObject
- the root object against which to evaluate the expressionvalue
- the new value- Throws:
EvaluationException
- if there is a problem during evaluation
-
compileExpression
public boolean 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.- Returns:
- whether this expression has been successfully compiled
-
revertToInterpreted
public void revertToInterpreted()Cause an expression to revert to being interpreted if it has been using a compiled form. It also resets the compilation attempt failure count (an expression is normally no longer considered compilable if it cannot be compiled after 100 attempts). -
getAST
Return the Abstract Syntax Tree for the expression. -
toStringAST
Produce a string representation of the Abstract Syntax Tree for the expression. This should ideally look like the input expression, but properly formatted since any unnecessary whitespace will have been discarded during the parse of the expression.- Returns:
- the string representation of the AST
-