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, e.g. 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)`.

  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`

- - ### Nested Class Summary

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

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and 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<ConstructorResolver>` | `getConstructorResolvers()` Return an empty list, always, since this context does not support the use of type references.
    `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 within this evaluation context.
    `void` | `setVariable(String name, Object value)` Set a named variable in this evaluation context to a specified value.

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

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

- - ### Method Detail

    - #### 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...)`

    - #### getConstructorResolvers

          public List<ConstructorResolver> getConstructorResolvers()

      Return an empty list, always, since this context does not support the use of type references.
      Specified by:
        `getConstructorResolvers` in interface `EvaluationContext`

    - #### 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)

      Description copied from interface: `EvaluationContext`
      Set a named variable in this evaluation context to a specified value.

      In contrast to `EvaluationContext.assignVariable(String, Supplier)`, this method should only be invoked programmatically when interacting directly with the `EvaluationContext` — for example, to provide initial configuration for the context.

      Specified by:
        `setVariable` in interface `EvaluationContext`
      Parameters:
        `name` - the name of the variable to set
        `value` - the value to be placed in the variable

    - #### lookupVariable

          @Nullable
          public Object lookupVariable(String name)

      Description copied from interface: `EvaluationContext`
      Look up a named variable within this evaluation context.
      Specified by:
        `lookupVariable` in interface `EvaluationContext`
      Parameters:
        `name` - the name of the variable to look up
      Returns:
        the value of the variable, 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()`