BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Java EE 7 Delivers Expression Language Enhancements

Java EE 7 Delivers Expression Language Enhancements

Leia em Português

This item in japanese

Lire ce contenu en français

Bookmarks

In the arsenal of enhancements that come with Java EE 7 is an overhaul of the Java Expression Language API known as Expression Language 3. Specified by JSR-341, the collection of enhancements to the EL API includes support for lambda expressions, static field and method access, as well as improvements for collection processing and a standalone processor mode.

Prior to Java EE 7, the Java Expression Language was a tightly coupled component of the JavaServer Faces and JavaServer Pages APIs. JSR-341 introduces the Java Expression Language as a distinct entity, decoupled — though still interoperable — from the view-layer APIs of Java EE.

The EL API now offers developers the ability to invoke ad-hoc Java EL. Creating an instance of the newly introduced ELProcessor class and invoking its eval method with a string that represents the script to be evaluated will return the result that can be used as a variable in the scope of the Java application.

ELProcessor el = new ELProcessor();
assert ((Integer)el.eval("a = [1, 2, 3]; a[1]")) == 2;
	

In addition to standalone mode, JSR-341 brings Java EE developers the ability to write lambda expressions. Lambda expressions are slated for standard edition Java with next year's Java 8 release, but for now, EL developers can utilize the lambda syntax in their scripts. At last year's JavaOne conference, JSR-341 specification lead Kin-man Chung noted that the specification's expert group consulted heavily with Java 8's Project Lambda developers to ensure a common API and future interoperability between Java EL and Java 8.

ELProcessor el = new ELProcessor();
assert ((Integer)(el.eval("((x,y) -> x+y)(4, 5)"))) == 9;

For working with collections, the new Java EL stream API includes operators such as forEach, reduce, filter, and map. Many of the features and concepts introduced to Java EL collection processing are also scheduled to be integrated with Java as part of Java 8. For now, however, developers can benefit from lazy and eager streams and lambda expressions as part of the enhancements from JSR-341.

ELProcessor el = new ELProcessor();
List list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
    list.add(i);
}
el.defineBean("list", list);
List processedList = (List)el.eval("list.stream().filter( x -> x <= 3).map(x -> x * 5).toList()");
assert processedList.get(0) == list.get(0);
assert processedList.get(1) == list.get(5);
assert processedList.get(2) == list.get(10);
assert processedList.get(3) == list.get(15);

Additionally featured as part of JSR-341 is the long awaited enhancement to Java EL that will allow developers to directly access static fields and methods. While the java.lang.* classes are pre-imported into all scripts by default, EL script developers can access static assets off of any class that is imported to the EL context through the ELManager asset on an ELProcessor instance.

ELProcessor el = new ELProcessor();
el.getELManager().importStatic("com.company.Class.STATIC_FIELD");
assert (el.eval("Math.random()") instanceof Double);
System.out.println(el.eval("STATIC_FIELD"));

The enhancements to the Java Expression Language that come as part of JSR-341 should prove to be a powerful assistant in JSP and JSF implementations, as well as in the newly offered standalone processing mode.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Could almost code a C# like LINQ for Java,....just missing closure feature (Java 8)!

    by Colbert Philippe,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    With this new lambda el, we can almost code an effective LINQ engine (as found in C#) but for Java. I think the closure feature would be necessary to make it an elegant and fully reusable construct.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT