Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1
What’s New with Expression Language in Java EE 7Reza RahmanJava EE/GlassFish [email protected]@reza_rahman
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2
Expression Language 3
EL first introduced in JSTL 1 Moved to JSP 2 Unified with JSF 1.2 in JSP 2.1 Now independent specification Used in JSF, JSP, CDI, BV and more
Overview
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3
Expression Language 3
Stand-alone API New operators Static field and method references Custom type converters Lambda expressions Collection construction Collection/lambda operations
New Feature Summary
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4
Expression Language 3
Provides standard ELResolvers and ELContext Direct expression evaluation
Support for stand-alone environments
ELProcessor elp = new ELProcessor();Object message = elp.eval(“’Welcome ‘ += user.name”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5
Expression Language 3
Defining functions and variables
Support for stand-alone environments
ELProcessor elp = new ELProcessor();elp.defineFunction(“func”, “ns”, “test.MyClass”, “funcname”);
elp.setVariable(“var”, “user.name”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6
Expression Language 3
Defining beans
Support for stand-alone environments
ELProcessor elp = new ELProcessor();ELManager elm = elp.getELManager();// Define a bean on the fly, into local bean repositoryelm.definBean(“name”, new Name(“Peter”));
// Add a map to bean look-upelm.addBeanNameResolver(new BeanNameResolver() { @Override public Object getBean(String name) { return myDB.get(name); }});
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7
Expression Language 3
Cannot use + because it is already used for arithmetic operation
String concatenation operator +=
ELProcessor elp = new ELProcessor();elp.eval(“’Welcome ‘ += user.name”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.8
Expression Language 3
Can assign to an existing bean Or dynamically create a new bean for the value Handled by ELResolver.setValue
Assignment operator =
ELProcessor elp = new ELProcessor();
elp.eval(“xx = user.name);elp.eval(“’Welcome ‘ += xx”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.9
Expression Language 3
Useful for side effects in complicated expressions
Semicolon operator ;
ELProcessor elp = new ELProcessor();
elp.eval(“xx = user.name; ‘Welcome ‘ += xx”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10
Expression Language 3
Allow plugging in custom type converters
Custom type converters
elp.getELManager().addELResolver(new TypeConverter() { @Override public Object convertToType(ELContext context, Object obj, Class<?> type) { if (obj instanceof String && type == MyBean.class) { context.setPropertyResolved(true); return new MyBean((String) obj); } return null; }}); Object val = elp.getValue("'John Doe'", MyBean.class);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11
Expression Language 3
Includes enum constants Class must be imported before its static members can be
referenced
Static field and method references
ELProcessor elp = new ELProcessor();ELManager elm = elp.getELManager();Elm.importClass(“com.acme.MyClass”);
elp.eval(“MyClass.staticName”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.12
Expression Language 3
java.lang package is pre-imported
Static field and method references
ELProcessor elp = new ELProcessor();
elp.eval("Boolean.TRUE");
elp.eval("Integer.numberOfTrailingZeros(16)");
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.13
Expression Language 3
Same syntax as Java SE 8 Behaves like an anonymous function Body consist of an EL expression Full access to EL environment in the body
Lambda expressions
x -> x+1(x,y) -> x+y() -> 64
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.14
Expression Language 3
The lambda is evaluated and discarded
Lambda expression: immediate evaluation
(x -> x+1)(10) 11((x,y)->x+y)(3,4) 7(()->64)() 64
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.15
Expression Language 3
A lambda can be named and evaluated indirectly Allows recursive invocation
Lambda expression: indirect evaluation
incr = x -> x+1; incr(10) 11fact = n->n==0? 1: n*fact(n-1); fact(5) 120
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.16
Expression Language 3
A lambda is encapsulated as javax.el.LambdaExpression Can be evaluated programmatically
Lambda expression: as javax.el.LambdaExpression
LambdaExpression lamb = (LambdaExpression)elp.eval(“x->x+1”);lamb.invoke(10);
elp.eval(“[1,2,3].stream().map(x->x+1).toList()”);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.17
Expression Language 3
Sets, lists, and maps can be constructed dynamically
Collection constructions
Set: {1,2,3}List: [“eenie”, “meenie”, “miney”, “mo”]Map: {‘one’:1, ‘two’:2}
Composite: {‘joe’: [‘m’,25], ‘amy’: [‘f’,18]}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18
Expression Language 3
Heavily influenced by JDK 8 libraries Implemented with lambda and method calls Allows fluent syntax/builder pattern About 20 operations
Collection operations: overview
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19
Expression Language 3
Operates on stream of collection objects Operations can be chained together to form a pipeline Pipeline: source stream, intermediate operations, terminal operation
To list history book titles:
Collection operations: stream and pipeline
books.stream().filter(b->b.category == ‘history’) .map(b->b.title) .toList()
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20
Expression Language 3Collection operations: examples
customers.stream().filter(c->c.country=='USA’) .flatMap(c->c.orders.stream()) .toList()
[1,3,5,2].stream().sorted().toList()people.stream().sorted((p,q)->p.name.compareTo(q.name)) .toList()
[1,2,3,4].stream().reduce(0, (a,i)->a+i))[1,2,3,4].stream().sum()
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.21
Summary
EL 3 brings important changes Both from a Java EE 7 and ecosystem perspective Try the APIs out, provide your feedback and get involved
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.22
Resources
EL project page– https://java.net/projects/el-spec/
Glassfish 4– https://glassfish.java.net/
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23
Top Related