SpelHelper provides additional functionality to work with Spring Expression Language (SpEL)
Go to file
Abhinav Sarkar f39d4eaf64 [maven-release-plugin] prepare for next development iteration 2012-05-18 00:44:05 +05:30
lib Added Javadocs and README 2010-05-27 01:51:03 +05:30
src Fixed an NPE in ImplicitPropertyAccessor due to putting null value in ConcurrentHashMap 2012-05-18 00:27:51 +05:30
.classpath Removed JUnit test. Added Scalatest spec. 2010-05-28 01:50:50 +05:30
.gitignore Updated to spring 3.0.6 2011-10-12 18:36:00 +05:30
.project Removed JUnit test. Added Scalatest spec. 2010-05-28 01:50:50 +05:30
COPYING Added license. Added more info in pom. 2010-05-27 20:24:57 +05:30
COPYING.LESSER Added license. Added more info in pom. 2010-05-27 20:24:57 +05:30
README.mdown Added Maven usage snippet 2011-10-13 01:06:46 +05:30
SpelHelper.iml Added more Scalatest specs 2010-05-28 13:24:11 +05:30
pom.xml [maven-release-plugin] prepare for next development iteration 2012-05-18 00:44:05 +05:30

README.mdown

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

SpelHelper is available under GNU Lesser General Public License (GNU LGPL).

Maven Usage

To use SpelHelper with Maven, add the following snippet in the dependencies section of your project's POM file:

<dependency>
  <groupId>net.abhinavsarkar</groupId>
  <artifactId>SpelHelper</artifactId>
  <version>1.1</version>
</dependency>

Functionalities

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 java.util.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.

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 java.lang.reflect.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)".

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')".

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.

For more details see the API Javadocs.