|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.abhinavsarkar.spelhelper.SpelHelper
public final class SpelHelper
SpelHelper provides additional functionalities to work with Spring Expression Language (SpEL).
The addition functionalities provided are:
Implicit Methods
Implicit methods allow one to registers methods with SpelHelper and attach them to particular classes. After that, when that method is called on an object of that particular class inside a SpEL expression, SpelHelper redirects the method call to the registered method.
Example: ImplicitMethods.sorted(List)
method is automatically
registered by SpelHelper. The class that the method should be invoked for
is the type of the first parameter of the method. In this case, the class is
List
.
So when an expression like "#list(1,4,2).sorted()"
is evaluated, the
ImplicitMethods.sorted(List)
method is invoked with the list as its
first parameter and its return value is used in further evaluation of the
expression.
See registerImplicitMethodsFromClass(Class)
.
Implicit Properties
Implicit properties allow one to treat no argument methods of an object as properties of the object. SpelHelper intercepts the property resolution of SpEL and if the property name is same as some no-arg method of the target object then it invokes the method on the object and provides its return value as the property value for further evaluation of the expression.
Example: Using implicit properties, the example of implicit methods can be
written as: "#list(1,4,2).sorted"
- dropping the parens - and it will return
the same value as the last example.
Implicit property resolution considers both the actual methods of the object and the implicit methods registered on the object's class.
Simplified extension functions
SpEL allows to register extension function on the context by providing a
name and a Method
object. SpelHelper simplifies this by taking a class
and registering all the public static
methods of the class which do not
have a void
return type. The methods are registered by their simple name.
Example: All the methods of ExtensionFunctions
class are automatically
registered by SpelHelper. Hence the method ExtensionFunctions.list(Object...)
can be called from inside a SpEL expression using the function call syntax:
"#list(1,2,3)
".
See registerFunctionsFromClass(Class)
.
Simplified constructors
SpEL allows calling constructors from inside a SpEL expression using the
new
operator. But they have to be called with their full name like:
"new org.example.Foo('bar')"
. SpelHelper simplifies this by taking a class
and registering all its public constructors to the SpEL context by their
simple name.
Example: After registering the org.example.Foo
class with SpelHelper, its
constructor can be called from inside a SpEL expression by: "new Foo('bar')"
.
See registerConstructorsFromClass(Class)
.
In addition to all the above functionalities, SpelHelper automatically registers
some extension functions and implicit methods which are always available in
the SpEL expressions evaluated through SpelHelper. See ExtensionFunctions
and ImplicitMethods
for further details.
Constructor Summary | |
---|---|
SpelHelper()
Creates an instance of SpelHelper. |
Method Summary | ||
---|---|---|
|
evalExpression(String expressionString,
EvaluationContext evaluationContext,
Class<T> desiredType)
Evaluates a SpEL expression expressionString in the provided
context evaluationContext and gives back a result of type
desiredType . |
|
|
evalExpression(String expressionString,
Object rootElement,
Class<T> desiredType)
Evaluates a SpEL expression expressionString in the context
of root element rootElement and gives back a result of type
desiredType . |
|
|
evalExpressions(String[] expressionStrings,
EvaluationContext evaluationContext,
Class<T> desiredType)
Evaluates multiple SpEL expressions and returns the result of the last expression. |
|
|
evalExpressions(String[] expressionStrings,
Object rootElement,
Class<T> desiredType)
Evaluates multiple SpEL expressions and returns the result of the last expression. |
|
static EvaluationContext |
getCurrentContext()
Returns the current evaluation context. |
|
Constructor<?> |
lookupImplicitConstructor(String lookup)
Looks up an implicit constructor registered with this instance. |
|
Method |
lookupImplicitMethod(String lookup)
Looks up an implicit method registered with this instance. |
|
SpelHelper |
registerConstructorsFromClass(Class<?> clazz)
Registers the public constructors of the class clazz so that they
can be called by their simple name from SpEL expressions. |
|
SpelHelper |
registerFunctionsFromClass(Class<?> clazz)
Registers the public static methods in the class clazz as functions
which can be called from SpEL expressions. |
|
SpelHelper |
registerImplicitMethodsFromClass(Class<?> clazz)
Registers the public static methods in the class clazz as implicit
methods for the class of the first parameter of the methods. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SpelHelper()
Method Detail |
---|
public SpelHelper registerImplicitMethodsFromClass(Class<?> clazz)
Registers the public static methods in the class clazz
as implicit
methods for the class of the first parameter of the methods.
Only registers the public static methods with non void return type and at least one argument.
clazz
- The class to register the methods from.
ImplicitMethods
public SpelHelper registerFunctionsFromClass(Class<?> clazz)
Registers the public static methods in the class clazz
as functions
which can be called from SpEL expressions.
The functions are registered with the simple name of the methods.
Only registers the public static methods with non void return type.
clazz
- The class to register the functions from.
ExtensionFunctions
public SpelHelper registerConstructorsFromClass(Class<?> clazz)
clazz
so that they
can be called by their simple name from SpEL expressions.
clazz
- The class to register the constructors from.
public <T> T evalExpression(String expressionString, Object rootElement, Class<T> desiredType)
expressionString
in the context
of root element rootElement
and gives back a result of type
desiredType
.
T
- The type of the result desired.expressionString
- The SpEL expression to evaluate.rootElement
- The root element in context of which the expression
is to be evaluated.desiredType
- The class of the result desired.
ExpressionParser.parseExpression(String)
,
Expression.getValue(EvaluationContext, Class)
public <T> T evalExpression(String expressionString, EvaluationContext evaluationContext, Class<T> desiredType)
expressionString
in the provided
context evaluationContext
and gives back a result of type
desiredType
.
T
- The type of the result desired.expressionString
- The SpEL expression to evaluate.evaluationContext
- The context in which the expression is to be evaluated.desiredType
- The class of the result desired.
ExpressionParser.parseExpression(String)
,
Expression.getValue(EvaluationContext, Class)
public <T> T evalExpressions(String[] expressionStrings, Object rootElement, Class<T> desiredType)
T
- The type of the result desired.expressionStrings
- The SpEL expressions to evaluate.rootElement
- The root element in context of which the expressions
are to be evaluated.desiredType
- The class of the result desired.
evalExpression(String, EvaluationContext, Class)
,
evalExpression(String, Object, Class)
public <T> T evalExpressions(String[] expressionStrings, EvaluationContext evaluationContext, Class<T> desiredType)
T
- The type of the result desired.expressionStrings
- The SpEL expressions to evaluate.evaluationContext
- The context in which the expression is to be evaluated.desiredType
- The class of the result desired.
evalExpression(String, EvaluationContext, Class)
,
evalExpression(String, Object, Class)
public Method lookupImplicitMethod(String lookup)
lookup
- key to lookup which should be of form:
method.getParameterTypes()[0].getName() + "." + method.getName()
public Constructor<?> lookupImplicitConstructor(String lookup)
lookup
- key to lookup which should be of form:
constructor.getDeclaringClass().getSimpleName()
+ Arrays.toString(constructor.getParameterTypes())
public static EvaluationContext getCurrentContext()
|
Code hosted at github | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |