Package org.springframework.expression.spel.support

# Class StandardEvaluationContext

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

All Implemented Interfaces:
  `EvaluationContext`

public class StandardEvaluationContext extends Object implements EvaluationContext
A powerful and highly configurable `EvaluationContext` implementation.

This context uses standard implementations of all applicable strategies, based on reflection to resolve properties, methods, and fields. Note, however, that you may need to manually configure a `StandardTypeLocator` with a specific `ClassLoader` to ensure that the SpEL expression parser is able to reliably locate user types. See `setTypeLocator(TypeLocator)` for details.

In addition to support for setting and looking up variables as defined in the `EvaluationContext` API, `StandardEvaluationContext` also provides support for registering and looking up functions. The `registerFunction(...)` methods provide a convenient way to register a function as a `Method` or a `MethodHandle`; however, a function can also be registered via `setVariable(String, Object)` or `setVariables(Map)`. Since functions share a namespace with the variables in this evaluation context, care must be taken to ensure that function names and variable names do not overlap.

For a simpler, builder-style context variant for data-binding purposes, consider using `SimpleEvaluationContext` instead which allows for opting into several SpEL features as needed by specific use cases.

Since:
  3.0
Author:
  Andy Clement, Juergen Hoeller, Sam Brannen, Stephane Nicoll
See Also:
  - `SimpleEvaluationContext`
  - `ReflectivePropertyAccessor`
  - `ReflectiveConstructorResolver`
  - `ReflectiveMethodResolver`
  - `StandardTypeLocator`
  - `StandardTypeConverter`
  - `StandardTypeComparator`
  - `StandardOperatorOverloader`

## Constructor Summary

Constructors
Constructor
Description
`StandardEvaluationContext()`
Create a `StandardEvaluationContext` with a null root object.
`StandardEvaluationContext(Object rootObject)`
Create a `StandardEvaluationContext` with the given root object.

## Method Summary

Modifier and Type
Method
Description
`void`
`addConstructorResolver(ConstructorResolver resolver)`
`void`
`addIndexAccessor(IndexAccessor indexAccessor)`
Add the supplied index accessor to this evaluation context.
`void`
`addMethodResolver(MethodResolver resolver)`
`void`
`addPropertyAccessor(PropertyAccessor accessor)`
`void`
`applyDelegatesTo(StandardEvaluationContext evaluationContext)`
Apply the internal delegates of this instance to the specified `evaluationContext`.
`BeanResolver`
`getBeanResolver()`
Return a bean resolver that can look up beans by name.
`List<ConstructorResolver>`
`getConstructorResolvers()`
Return a list of resolvers that will be asked in turn to locate a constructor.
`List<IndexAccessor>`
`getIndexAccessors()`
Get the list of index accessors configured in this evaluation context.
`List<MethodResolver>`
`getMethodResolvers()`
Return a list of resolvers that will be asked in turn to locate a method.
`OperatorOverloader`
`getOperatorOverloader()`
Return an operator overloader that may support mathematical operations between more than the standard set of types.
`List<PropertyAccessor>`
`getPropertyAccessors()`
Return a list of accessors that will be asked in turn to read/write a property.
`TypedValue`
`getRootObject()`
Return the default root context object against which unqualified properties, methods, etc.
`TypeComparator`
`getTypeComparator()`
Return a type comparator for comparing pairs of objects for equality.
`TypeConverter`
`getTypeConverter()`
Return a type converter that can convert (or coerce) a value from one type to another.
`TypeLocator`
`getTypeLocator()`
Get the configured `TypeLocator` that will be used to find types, either by short or fully-qualified name.
`Object`
`lookupVariable(String name)`
Look up a named variable or function within this evaluation context.
`void`
`registerFunction(String name, MethodHandle methodHandle)`
Register the specified `MethodHandle` as a SpEL function.
`void`
`registerFunction(String name, Method method)`
Register the specified `Method` as a SpEL function.
`void`
`registerMethodFilter(Class<?> type, MethodFilter filter)`
Register a `MethodFilter` which will be called during method resolution for the specified type.
`boolean`
`removeConstructorResolver(ConstructorResolver resolver)`
`boolean`
`removeIndexAccessor(IndexAccessor indexAccessor)`
Remove the supplied index accessor from this evaluation context.
`boolean`
`removeMethodResolver(MethodResolver methodResolver)`
`boolean`
`removePropertyAccessor(PropertyAccessor accessor)`
`void`
`setBeanResolver(BeanResolver beanResolver)`
`void`
`setConstructorResolvers(List<ConstructorResolver> constructorResolvers)`
`void`
`setIndexAccessors(List<IndexAccessor> indexAccessors)`
Set the list of index accessors to use in this evaluation context.
`void`
`setMethodResolvers(List<MethodResolver> methodResolvers)`
`void`
`setOperatorOverloader(OperatorOverloader operatorOverloader)`
`void`
`setPropertyAccessors(List<PropertyAccessor> propertyAccessors)`
`void`
`setRootObject(Object rootObject)`
`void`
`setRootObject(Object rootObject, org.springframework.core.convert.TypeDescriptor typeDescriptor)`
`void`
`setTypeComparator(TypeComparator typeComparator)`
`void`
`setTypeConverter(TypeConverter typeConverter)`
`void`
`setTypeLocator(TypeLocator typeLocator)`
Set the `TypeLocator` to use to find types, either by short or fully-qualified name.
`void`
`setVariable(String name, Object value)`
Set a named variable in this evaluation context to a specified value.
`void`
`setVariables(Map<String,Object> variables)`
Set multiple named variables in this evaluation context to the specified values.

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

`assignVariable, isAssignmentEnabled`

## Constructor Details

### StandardEvaluationContext

public StandardEvaluationContext()
Create a `StandardEvaluationContext` with a null root object.

### StandardEvaluationContext

public StandardEvaluationContext(@Nullable Object rootObject)
Create a `StandardEvaluationContext` with the given root object.

Parameters:
  `rootObject` - the root object to use
See Also:
  - `setRootObject(java.lang.Object, org.springframework.core.convert.TypeDescriptor)`

## Method Details

### setRootObject

public void setRootObject(@Nullable Object rootObject, org.springframework.core.convert.TypeDescriptor typeDescriptor)

### setRootObject

public void setRootObject(@Nullable Object rootObject)

### getRootObject

public TypedValue getRootObject()
Description copied from interface: `EvaluationContext`
Return the default root context object against which unqualified properties, methods, etc. should be resolved.

This can be overridden when evaluating an expression.

Specified by:
  `getRootObject` in interface `EvaluationContext`

### setPropertyAccessors

public void setPropertyAccessors(List<PropertyAccessor> propertyAccessors)

### getPropertyAccessors

public List<PropertyAccessor> getPropertyAccessors()
Description copied from interface: `EvaluationContext`
Return a list of accessors that will be asked in turn to read/write a property.

The default implementation returns an empty list.

Specified by:
  `getPropertyAccessors` in interface `EvaluationContext`

### addPropertyAccessor

public void addPropertyAccessor(PropertyAccessor accessor)

### removePropertyAccessor

public boolean removePropertyAccessor(PropertyAccessor accessor)

### setIndexAccessors

public void setIndexAccessors(List<IndexAccessor> indexAccessors)
Set the list of index accessors to use in this evaluation context.

Replaces any previously configured index accessors.

Since:
  6.2
See Also:
  - `getIndexAccessors()`
  - `addIndexAccessor(IndexAccessor)`
  - `removeIndexAccessor(IndexAccessor)`

### getIndexAccessors

public List<IndexAccessor> getIndexAccessors()
Get the list of index accessors configured in this evaluation context.

Specified by:
  `getIndexAccessors` in interface `EvaluationContext`
Since:
  6.2
See Also:
  - `setIndexAccessors(List)`
  - `addIndexAccessor(IndexAccessor)`
  - `removeIndexAccessor(IndexAccessor)`

### addIndexAccessor

public void addIndexAccessor(IndexAccessor indexAccessor)
Add the supplied index accessor to this evaluation context.

Parameters:
  `indexAccessor` - the index accessor to add
Since:
  6.2
See Also:
  - `getIndexAccessors()`
  - `setIndexAccessors(List)`
  - `removeIndexAccessor(IndexAccessor)`

### removeIndexAccessor

public boolean removeIndexAccessor(IndexAccessor indexAccessor)
Remove the supplied index accessor from this evaluation context.

Parameters:
  `indexAccessor` - the index accessor to remove
Returns:
  `true` if the index accessor was removed, `false` if the index accessor was not configured in this evaluation context
Since:
  6.2
See Also:
  - `getIndexAccessors()`
  - `setIndexAccessors(List)`
  - `addIndexAccessor(IndexAccessor)`

### setConstructorResolvers

public void setConstructorResolvers(List<ConstructorResolver> constructorResolvers)

### getConstructorResolvers

public List<ConstructorResolver> getConstructorResolvers()
Description copied from interface: `EvaluationContext`
Return a list of resolvers that will be asked in turn to locate a constructor.

The default implementation returns an empty list.

Specified by:
  `getConstructorResolvers` in interface `EvaluationContext`

### addConstructorResolver

public void addConstructorResolver(ConstructorResolver resolver)

### removeConstructorResolver

public boolean removeConstructorResolver(ConstructorResolver resolver)

### setMethodResolvers

public void setMethodResolvers(List<MethodResolver> methodResolvers)

### getMethodResolvers

public List<MethodResolver> getMethodResolvers()
Description copied from interface: `EvaluationContext`
Return a list of resolvers that will be asked in turn to locate a method.

The default implementation returns an empty list.

Specified by:
  `getMethodResolvers` in interface `EvaluationContext`

### addMethodResolver

public void addMethodResolver(MethodResolver resolver)

### removeMethodResolver

public boolean removeMethodResolver(MethodResolver methodResolver)

### setBeanResolver

public void setBeanResolver(BeanResolver beanResolver)

### getBeanResolver

@Nullable public BeanResolver getBeanResolver()
Description copied from interface: `EvaluationContext`
Return a bean resolver that can look up beans by name.

Specified by:
  `getBeanResolver` in interface `EvaluationContext`

### setTypeLocator

public void setTypeLocator(TypeLocator typeLocator)
Set the `TypeLocator` to use to find types, either by short or fully-qualified name.

By default, a `StandardTypeLocator` will be used.

NOTE: Even if a `StandardTypeLocator` is sufficient, you may need to manually configure a `StandardTypeLocator` with a specific `ClassLoader` to ensure that the SpEL expression parser is able to reliably locate user types.

Parameters:
  `typeLocator` - the `TypeLocator` to use
See Also:
  - `StandardTypeLocator(ClassLoader)`
  - `getTypeLocator()`

### getTypeLocator

public TypeLocator getTypeLocator()
Get the configured `TypeLocator` that will be used to find types, either by short or fully-qualified name.

See `setTypeLocator(TypeLocator)` for further details.

Specified by:
  `getTypeLocator` in interface `EvaluationContext`
See Also:
  - `setTypeLocator(TypeLocator)`

### setTypeConverter

public void setTypeConverter(TypeConverter typeConverter)

### getTypeConverter

public TypeConverter getTypeConverter()
Description copied from interface: `EvaluationContext`
Return a type converter that can convert (or coerce) a value from one type to another.

Specified by:
  `getTypeConverter` in interface `EvaluationContext`

### setTypeComparator

public void setTypeComparator(TypeComparator typeComparator)

### getTypeComparator

public TypeComparator getTypeComparator()
Description copied from interface: `EvaluationContext`
Return a type comparator for comparing pairs of objects for equality.

Specified by:
  `getTypeComparator` in interface `EvaluationContext`

### setOperatorOverloader

public void setOperatorOverloader(OperatorOverloader operatorOverloader)

### getOperatorOverloader

public OperatorOverloader getOperatorOverloader()
Description copied from interface: `EvaluationContext`
Return an operator overloader that may support mathematical operations between more than the standard set of types.

Specified by:
  `getOperatorOverloader` in interface `EvaluationContext`

### setVariable

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

If the specified `name` is `null`, it will be ignored. If the specified `value` is `null`, the named variable will be removed from this evaluation context.

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

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 to set
  `value` - the value to be placed in the variable
See Also:
  - `setVariables(Map)`
  - `registerFunction(String, Method)`
  - `registerFunction(String, MethodHandle)`
  - `lookupVariable(String)`

### setVariables

public void setVariables(Map<String,Object> variables)
Set multiple named variables in this evaluation context to the specified values.

This is a convenience variant of `setVariable(String, Object)`.

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

Parameters:
  `variables` - the names and values of the variables to set
See Also:
  - `setVariable(String, Object)`

### registerFunction

public void registerFunction(String name, Method method)
Register the specified `Method` as a SpEL function.

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

Parameters:
  `name` - the name of the function
  `method` - the `Method` to register
See Also:
  - `registerFunction(String, MethodHandle)`

### registerFunction

public void registerFunction(String name, MethodHandle methodHandle)
Register the specified `MethodHandle` as a SpEL function.

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

Parameters:
  `name` - the name of the function
  `methodHandle` - the `MethodHandle` to register
Since:
  6.1
See Also:
  - `registerFunction(String, Method)`

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

### registerMethodFilter

public void registerMethodFilter(Class<?> type, MethodFilter filter) throws IllegalStateException
Register a `MethodFilter` which will be called during method resolution for the specified type.

The `MethodFilter` may remove methods and/or sort the methods which will then be used by SpEL as the candidates to look through for a match.

Parameters:
  `type` - the type for which the filter should be called
  `filter` - a `MethodFilter`, or `null` to unregister a filter for the type
Throws:
  `IllegalStateException` - if the `ReflectiveMethodResolver` is not in use

### applyDelegatesTo

public void applyDelegatesTo(StandardEvaluationContext evaluationContext)
Apply the internal delegates of this instance to the specified `evaluationContext`. Typically invoked right after the new context instance has been created to reuse the delegates. Does not modify the root object or any registered variables or functions.

Parameters:
  `evaluationContext` - the evaluation context to update
Since:
  6.1.1