A parsed expression, ready to be evaluated as often as needed.
Parsing costs more than evaluating, so parse a text once and keep the expression. It holds no state of its own: the values it reads come from the EvaluationContext and the root object given to each call, so one expression can be shared and evaluated against different objects.
The parser returns a SpelExpression for one SpEL expression, and a LiteralExpression or CompositeStringExpression for a template.
// A pricing rule parsed once, evaluated for each order line.
final expression = SpelExpressionParser().parseExpression('qty * 2');
expression.getValue(StandardEvaluationContext({'qty': 3})); // 6
expression.getValueAs<String>(null, {'qty': 4}); // '8'
Spring (Java):
Expression expression = parser.parseExpression("qty * 2");
Integer six = expression.getValue(context, Integer.class);
String eight = expression.getValue(rootObject, String.class);
- Implementers
Properties
- expressionString → String
-
The text the expression was parsed from (the whole template for a
template).
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getValue(
[EvaluationContext? context, Object? rootObject]) → Object? - Evaluates the expression and returns its value.
-
getValueAs<
T> ([EvaluationContext? context, Object? rootObject]) → T -
Evaluates like getValue and returns the value as a
T. -
getValueType(
[EvaluationContext? context, Object? rootObject]) → Type? -
The runtime type of the value, or
nullwhen the value isnull(Stringfor a template). -
isWritable(
[EvaluationContext? context, Object? rootObject]) → bool - Whether setValue would store a value: the expression names a writable property, variable or element and the context allows assignment.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setValue(
EvaluationContext? context, Object? rootObject, Object? value) → void -
Stores
valuein the property, variable or element the expression names, such asuser.name,#countoritems[0]. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited