@ -48,6 +48,16 @@ import org.springframework.util.Assert;
* to reliably locate user types . See { @link # setTypeLocator ( TypeLocator ) } for
* to reliably locate user types . See { @link # setTypeLocator ( TypeLocator ) } for
* details .
* details .
*
*
* < p > In addition to support for setting and looking up variables as defined in
* the { @link EvaluationContext } API , { @code StandardEvaluationContext } also
* provides support for registering and looking up functions . The
* { @code registerFunction ( . . . ) } methods provide a convenient way to register a
* function as a { @link Method } or a { @link MethodHandle } ; however , a function
* can also be registered via { @link # setVariable ( String , Object ) } or
* { @link # 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 .
*
* < p > For a simpler , builder - style context variant for data - binding purposes ,
* < p > For a simpler , builder - style context variant for data - binding purposes ,
* consider using { @link SimpleEvaluationContext } instead which allows for
* consider using { @link SimpleEvaluationContext } instead which allows for
* opting into several SpEL features as needed by specific use cases .
* opting into several SpEL features as needed by specific use cases .
@ -253,6 +263,25 @@ public class StandardEvaluationContext implements EvaluationContext {
return this . operatorOverloader ;
return this . operatorOverloader ;
}
}
/ * *
* Set a named variable in this evaluation context to a specified value .
* < p > If the specified { @code name } is { @code null } , it will be ignored . If
* the specified { @code value } is { @code null } , the named variable will be
* removed from this evaluation context .
* < p > In contrast to { @link # assignVariable ( String , java . util . function . Supplier ) } ,
* this method should only be invoked programmatically when interacting directly
* with the { @code EvaluationContext } & mdash ; for example , to provide initial
* configuration for the context .
* < p > Note that variables and functions share a common namespace in this
* evaluation context . See the { @linkplain StandardEvaluationContext
* class - level documentation } for details .
* @param name the name of the variable to set
* @param value the value to be placed in the variable
* @see # setVariables ( Map )
* @see # registerFunction ( String , Method )
* @see # registerFunction ( String , MethodHandle )
* @see # lookupVariable ( String )
* /
@Override
@Override
public void setVariable ( @Nullable String name , @Nullable Object value ) {
public void setVariable ( @Nullable String name , @Nullable Object value ) {
// For backwards compatibility, we ignore null names here...
// For backwards compatibility, we ignore null names here...
@ -271,6 +300,9 @@ public class StandardEvaluationContext implements EvaluationContext {
/ * *
/ * *
* Set multiple named variables in this evaluation context to the specified values .
* Set multiple named variables in this evaluation context to the specified values .
* < p > This is a convenience variant of { @link # setVariable ( String , Object ) } .
* < p > This is a convenience variant of { @link # setVariable ( String , Object ) } .
* < p > Note that variables and functions share a common namespace in this
* evaluation context . See the { @linkplain StandardEvaluationContext
* class - level documentation } for details .
* @param variables the names and values of the variables to set
* @param variables the names and values of the variables to set
* @see # setVariable ( String , Object )
* @see # setVariable ( String , Object )
* /
* /
@ -280,9 +312,9 @@ public class StandardEvaluationContext implements EvaluationContext {
/ * *
/ * *
* Register the specified { @link Method } as a SpEL function .
* Register the specified { @link Method } as a SpEL function .
* < p > Note : Function names share a namespace with the variables in this
* < p > Note that variables and functions share a common namespace in this
* evaluation context , as populated by { @link # setVariable ( String , Object ) } .
* evaluation context . See the { @linkplain StandardEvaluationContext
* Make sure that specified function names and variable names do not overlap .
* class - level documentation } for details .
* @param name the name of the function
* @param name the name of the function
* @param method the { @code Method } to register
* @param method the { @code Method } to register
* @see # registerFunction ( String , MethodHandle )
* @see # registerFunction ( String , MethodHandle )
@ -293,9 +325,9 @@ public class StandardEvaluationContext implements EvaluationContext {
/ * *
/ * *
* Register the specified { @link MethodHandle } as a SpEL function .
* Register the specified { @link MethodHandle } as a SpEL function .
* < p > Note : Function names share a namespace with the variables in this
* < p > Note that variables and functions share a common namespace in this
* evaluation context , as populated by { @link # setVariable ( String , Object ) } .
* evaluation context . See the { @linkplain StandardEvaluationContext
* Make sure that specified function names and variable names do not overlap .
* class - level documentation } for details .
* @param name the name of the function
* @param name the name of the function
* @param methodHandle the { @link MethodHandle } to register
* @param methodHandle the { @link MethodHandle } to register
* @since 6 . 1
* @since 6 . 1
@ -305,6 +337,14 @@ public class StandardEvaluationContext implements EvaluationContext {
this . variables . put ( name , methodHandle ) ;
this . variables . put ( name , methodHandle ) ;
}
}
/ * *
* Look up a named variable or function within this evaluation context .
* < p > Note that variables and functions share a common namespace in this
* evaluation context . See the { @linkplain StandardEvaluationContext
* class - level documentation } for details .
* @param name the name of the variable or function to look up
* @return the value of the variable or function , or { @code null } if not found
* /
@Override
@Override
@Nullable
@Nullable
public Object lookupVariable ( String name ) {
public Object lookupVariable ( String name ) {
@ -333,9 +373,9 @@ public class StandardEvaluationContext implements EvaluationContext {
/ * *
/ * *
* Apply the internal delegates of this instance to the specified
* Apply the internal delegates of this instance to the specified
* { @code evaluationContext } . Typically invoked right after the new context
* { @code evaluationContext } . Typically invoked right after the new context
* instance has been created to reuse the delegates . Do not modify the
* instance has been created to reuse the delegates . Does not modify the
* { @linkplain # setRootObject ( Object ) root object } or any registered
* { @linkplain # setRootObject ( Object ) root object } or any registered
* { @linkplain # setVariables ( Map ) variable s } .
* { @linkplain # setVariable variables or function s } .
* @param evaluationContext the evaluation context to update
* @param evaluationContext the evaluation context to update
* @since 6 . 1 . 1
* @since 6 . 1 . 1
* /
* /