Package org.springframework.expression.spel.support

# Class SimpleEvaluationContext

java.lang.Object
org.springframework.expression.spel.support.SimpleEvaluationContext

All Implemented Interfaces:
  `EvaluationContext`

public final class SimpleEvaluationContext extends Object implements EvaluationContext
A basic implementation of `EvaluationContext` that focuses on a subset of essential SpEL features and customization options, targeting simple condition evaluation and in particular data binding scenarios.

In many cases, the full extent of the SpEL language is not required and should be meaningfully restricted. Examples include but are not limited to data binding expressions, property-based filters, and others. To that effect, `SimpleEvaluationContext` is tailored to support only a subset of the SpEL language syntax, for example, excluding references to Java types, constructors, and bean references.

When creating a `SimpleEvaluationContext` you need to choose the level of support that you need for data binding in SpEL expressions:

- Data binding for read-only access
- Data binding for read and write access
- A custom `PropertyAccessor` (typically not reflection-based), potentially combined with a `DataBindingPropertyAccessor`

Conveniently, `forReadOnlyDataBinding()` enables read-only access to properties via `DataBindingPropertyAccessor`. Similarly, `forReadWriteDataBinding()` enables read and write access to properties. Alternatively, configure custom accessors via `forPropertyAccessors(org.springframework.expression.PropertyAccessor...)`, potentially disable assignment, and optionally activate method resolution and/or a type converter through the builder.

Note that `SimpleEvaluationContext` is typically not configured with a default root object. Instead it is meant to be created once and used repeatedly through `getValue` calls on a predefined `Expression` with both an `EvaluationContext` and a root object as arguments: `Expression.getValue(EvaluationContext, Object)`.

In addition to support for setting and looking up variables as defined in the `EvaluationContext` API, `SimpleEvaluationContext` also provides support for registering and looking up functions as variables. Since functions share a common namespace with the variables in this evaluation context, care must be taken to ensure that function names and variable names do not overlap.

For more power and flexibility, in particular for internal configuration scenarios, consider using `StandardEvaluationContext` instead.

Since:
  4.3.15
Author:
  Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
See Also:
  - `forReadOnlyDataBinding()`
  - `forReadWriteDataBinding()`
  - `forPropertyAccessors(org.springframework.expression.PropertyAccessor...)`
  - `StandardEvaluationContext`
  - `StandardTypeConverter`
  - `DataBindingPropertyAccessor`
  - `DataBindingMethodResolver`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static final class`
`SimpleEvaluationContext.Builder`
Builder for `SimpleEvaluationContext`.

## Method Summary

Modifier and Type
Method
Description
`TypedValue`
`assignVariable(String name, Supplier<TypedValue> valueSupplier)`
`SimpleEvaluationContext` does not support variable assignment within expressions.
`static SimpleEvaluationContext.Builder`
`forPropertyAccessors(PropertyAccessor... accessors)`
Create a `SimpleEvaluationContext` for the specified `PropertyAccessor` delegates: typically a custom `PropertyAccessor` specific to a use case — for example, for attribute resolution in a custom data structure — potentially combined with a `DataBindingPropertyAccessor` if property dereferences are needed as well.
`static SimpleEvaluationContext.Builder`
`forReadOnlyDataBinding()`
Create a `SimpleEvaluationContext` for read-only access to public properties via `DataBindingPropertyAccessor`.
`static SimpleEvaluationContext.Builder`
`forReadWriteDataBinding()`
Create a `SimpleEvaluationContext` for read-write access to public properties via `DataBindingPropertyAccessor`.
`BeanResolver`
`getBeanResolver()`
`SimpleEvaluationContext` does not support the use of bean references.
`List<IndexAccessor>`
`getIndexAccessors()`
Return the specified `IndexAccessor` delegates, if any.
`List<MethodResolver>`
`getMethodResolvers()`
Return the specified `MethodResolver` delegates, if any.
`OperatorOverloader`
`getOperatorOverloader()`
Return an instance of `StandardOperatorOverloader`.
`List<PropertyAccessor>`
`getPropertyAccessors()`
Return the specified `PropertyAccessor` delegates, if any.
`TypedValue`
`getRootObject()`
Return the specified root object, if any.
`TypeComparator`
`getTypeComparator()`
Return an instance of `StandardTypeComparator`.
`TypeConverter`
`getTypeConverter()`
The configured `TypeConverter`.
`TypeLocator`
`getTypeLocator()`
`SimpleEvaluationContext` does not support use of type references.
`boolean`
`isAssignmentEnabled()`
Determine if assignment is enabled within expressions evaluated by this evaluation context.
`Object`
`lookupVariable(String name)`
Look up a named variable or function within this evaluation context.
`void`
`setVariable(String name, Object value)`
Set a named variable or function in this evaluation context to the specified value.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.expression.EvaluationContext

`getConstructorResolvers`

## Method Details

### getRootObject

public TypedValue getRootObject()
Return the specified root object, if any.

Specified by:
  `getRootObject` in interface `EvaluationContext`

### getPropertyAccessors

public List<PropertyAccessor> getPropertyAccessors()
Return the specified `PropertyAccessor` delegates, if any.

Specified by:
  `getPropertyAccessors` in interface `EvaluationContext`
See Also:
  - `forPropertyAccessors(org.springframework.expression.PropertyAccessor...)`

### getIndexAccessors

public List<IndexAccessor> getIndexAccessors()
Return the specified `IndexAccessor` delegates, if any.

Specified by:
  `getIndexAccessors` in interface `EvaluationContext`
Since:
  6.2
See Also:
  - `SimpleEvaluationContext.Builder.withIndexAccessors(IndexAccessor...)`

### getMethodResolvers

public List<MethodResolver> getMethodResolvers()
Return the specified `MethodResolver` delegates, if any.

Specified by:
  `getMethodResolvers` in interface `EvaluationContext`
See Also:
  - `SimpleEvaluationContext.Builder.withMethodResolvers(org.springframework.expression.MethodResolver...)`

### getBeanResolver

@Nullable public BeanResolver getBeanResolver()
`SimpleEvaluationContext` does not support the use of bean references.

Specified by:
  `getBeanResolver` in interface `EvaluationContext`
Returns:
  always `null`

### getTypeLocator

public TypeLocator getTypeLocator()
`SimpleEvaluationContext` does not support use of type references.

Specified by:
  `getTypeLocator` in interface `EvaluationContext`
Returns:
  `TypeLocator` implementation that raises a `SpelEvaluationException` with `SpelMessage.TYPE_NOT_FOUND`

### getTypeConverter

public TypeConverter getTypeConverter()
The configured `TypeConverter`.

By default this is `StandardTypeConverter`.

Specified by:
  `getTypeConverter` in interface `EvaluationContext`
See Also:
  - `SimpleEvaluationContext.Builder.withTypeConverter(org.springframework.expression.TypeConverter)`
  - `SimpleEvaluationContext.Builder.withConversionService(org.springframework.core.convert.ConversionService)`

### getTypeComparator

public TypeComparator getTypeComparator()
Return an instance of `StandardTypeComparator`.

Specified by:
  `getTypeComparator` in interface `EvaluationContext`

### getOperatorOverloader

public OperatorOverloader getOperatorOverloader()
Return an instance of `StandardOperatorOverloader`.

Specified by:
  `getOperatorOverloader` in interface `EvaluationContext`

### assignVariable

public TypedValue assignVariable(String name, Supplier<TypedValue> valueSupplier)
`SimpleEvaluationContext` does not support variable assignment within expressions.

Specified by:
  `assignVariable` in interface `EvaluationContext`
Parameters:
  `name` - the name of the variable to assign
  `valueSupplier` - the supplier of the value to be assigned to the variable
Returns:
  a `TypedValue` wrapping the assigned value
Throws:
  `SpelEvaluationException` - with `SpelMessage.VARIABLE_ASSIGNMENT_NOT_SUPPORTED`
Since:
  5.2.24

### setVariable

public void setVariable(String name, @Nullable Object value)
Set a named variable or function in this evaluation context to the specified value.

A function can be registered as a `Method` or a `MethodHandle`.

Note that variables and functions share a common namespace in this evaluation context. See the class-level documentation for details.

Specified by:
  `setVariable` in interface `EvaluationContext`
Parameters:
  `name` - the name of the variable or function to set
  `value` - the value to be placed in the variable or function
See Also:
  - `lookupVariable(String)`

### lookupVariable

@Nullable public Object lookupVariable(String name)
Look up a named variable or function within this evaluation context.

Note that variables and functions share a common namespace in this evaluation context. See the class-level documentation for details.

Specified by:
  `lookupVariable` in interface `EvaluationContext`
Parameters:
  `name` - the name of the variable or function to look up
Returns:
  the value of the variable or function, or `null` if not found

### isAssignmentEnabled

public boolean isAssignmentEnabled()
Determine if assignment is enabled within expressions evaluated by this evaluation context.

If this method returns `false`, the assignment (`=`), increment (`++`), and decrement (`--`) operators are disabled.

Specified by:
  `isAssignmentEnabled` in interface `EvaluationContext`
Returns:
  `true` if assignment is enabled; `false` otherwise
Since:
  5.3.38
See Also:
  - `forReadOnlyDataBinding()`
  - `SimpleEvaluationContext.Builder.withAssignmentDisabled()`

### forPropertyAccessors

public static SimpleEvaluationContext.Builder forPropertyAccessors(PropertyAccessor... accessors)
Create a `SimpleEvaluationContext` for the specified `PropertyAccessor` delegates: typically a custom `PropertyAccessor` specific to a use case — for example, for attribute resolution in a custom data structure — potentially combined with a `DataBindingPropertyAccessor` if property dereferences are needed as well.

By default, assignment is enabled within expressions evaluated by the context created via this factory method; however, assignment can be disabled via `SimpleEvaluationContext.Builder.withAssignmentDisabled()`.

Parameters:
  `accessors` - the accessor delegates to use
See Also:
  - `DataBindingPropertyAccessor.forReadOnlyAccess()`
  - `DataBindingPropertyAccessor.forReadWriteAccess()`
  - `isAssignmentEnabled()`
  - `SimpleEvaluationContext.Builder.withAssignmentDisabled()`

### forReadOnlyDataBinding

public static SimpleEvaluationContext.Builder forReadOnlyDataBinding()
Create a `SimpleEvaluationContext` for read-only access to public properties via `DataBindingPropertyAccessor`.

Assignment is disabled within expressions evaluated by the context created via this factory method.

See Also:
  - `DataBindingPropertyAccessor.forReadOnlyAccess()`
  - `forPropertyAccessors(org.springframework.expression.PropertyAccessor...)`
  - `isAssignmentEnabled()`
  - `SimpleEvaluationContext.Builder.withAssignmentDisabled()`

### forReadWriteDataBinding

public static SimpleEvaluationContext.Builder forReadWriteDataBinding()
Create a `SimpleEvaluationContext` for read-write access to public properties via `DataBindingPropertyAccessor`.

By default, assignment is enabled within expressions evaluated by the context created via this factory method. Assignment can be disabled via `SimpleEvaluationContext.Builder.withAssignmentDisabled()`; however, it is preferable to use `forReadOnlyDataBinding()` if you desire read-only access.

See Also:
  - `DataBindingPropertyAccessor.forReadWriteAccess()`
  - `forPropertyAccessors(org.springframework.expression.PropertyAccessor...)`
  - `isAssignmentEnabled()`
  - `SimpleEvaluationContext.Builder.withAssignmentDisabled()`