net.abhinavsarkar.spelhelper
Class SpelHelper

java.lang.Object
  extended by net.abhinavsarkar.spelhelper.SpelHelper

public final class SpelHelper
extends Object

SpelHelper provides additional functionalities to work with Spring Expression Language (SpEL).

The addition functionalities provided are:

  1. Implicit methods
  2. Implicit properties
  3. Simplified extension functions
  4. Simplified constructors

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.

Author:
Abhinav Sarkar abhinav@abhinavsarkar.net

Constructor Summary
SpelHelper()
          Creates an instance of SpelHelper.
 
Method Summary
<T> T
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.
<T> T
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.
<T> T
evalExpressions(String[] expressionStrings, EvaluationContext evaluationContext, Class<T> desiredType)
          Evaluates multiple SpEL expressions and returns the result of the last expression.
<T> T
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

SpelHelper

public SpelHelper()
Creates an instance of SpelHelper.

Method Detail

registerImplicitMethodsFromClass

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.

Parameters:
clazz - The class to register the methods from.
Returns:
The current instance of SpelHelper. This is for chaining the methods calls.
See Also:
ImplicitMethods

registerFunctionsFromClass

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.

Parameters:
clazz - The class to register the functions from.
Returns:
The current instance of SpelHelper. This is for chaining the methods calls.
See Also:
ExtensionFunctions

registerConstructorsFromClass

public 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.

Parameters:
clazz - The class to register the constructors from.
Returns:
The current instance of SpelHelper. This is for chaining the methods calls.

evalExpression

public <T> T 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.

Type Parameters:
T - The type of the result desired.
Parameters:
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.
Returns:
The result of the evaluation of the expression.
See Also:
ExpressionParser.parseExpression(String), Expression.getValue(EvaluationContext, Class)

evalExpression

public <T> T 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.

Type Parameters:
T - The type of the result desired.
Parameters:
expressionString - The SpEL expression to evaluate.
evaluationContext - The context in which the expression is to be evaluated.
desiredType - The class of the result desired.
Returns:
The result of the evaluation of the expression.
See Also:
ExpressionParser.parseExpression(String), Expression.getValue(EvaluationContext, Class)

evalExpressions

public <T> T evalExpressions(String[] expressionStrings,
                             Object rootElement,
                             Class<T> desiredType)
Evaluates multiple SpEL expressions and returns the result of the last expression.

Type Parameters:
T - The type of the result desired.
Parameters:
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.
Returns:
The result of the evaluation of the last expression.
See Also:
evalExpression(String, EvaluationContext, Class), evalExpression(String, Object, Class)

evalExpressions

public <T> T evalExpressions(String[] expressionStrings,
                             EvaluationContext evaluationContext,
                             Class<T> desiredType)
Evaluates multiple SpEL expressions and returns the result of the last expression.

Type Parameters:
T - The type of the result desired.
Parameters:
expressionStrings - The SpEL expressions to evaluate.
evaluationContext - The context in which the expression is to be evaluated.
desiredType - The class of the result desired.
Returns:
The result of the evaluation of the last expression.
See Also:
evalExpression(String, EvaluationContext, Class), evalExpression(String, Object, Class)

lookupImplicitMethod

public Method lookupImplicitMethod(String lookup)
Looks up an implicit method registered with this instance.

Parameters:
lookup - key to lookup which should be of form: method.getParameterTypes()[0].getName() + "." + method.getName()
Returns:
The registered method if found, else null.

lookupImplicitConstructor

public Constructor<?> lookupImplicitConstructor(String lookup)
Looks up an implicit constructor registered with this instance.

Parameters:
lookup - key to lookup which should be of form: constructor.getDeclaringClass().getSimpleName() + Arrays.toString(constructor.getParameterTypes())
Returns:
The registered constructor if found, else null.

getCurrentContext

public static EvaluationContext getCurrentContext()
Returns the current evaluation context. Null if there is no context.

Returns:
The current evaluation context.

Code hosted at github

Copyright © 2010-2011. All Rights Reserved.