What’s New with Expression Language in Java EE 7

15,283 views

Published on

This fast-faced, code-centric lightning talk covers the new Expression Language (EL) 3 API.

EL has long been a very important part of APIs like JSTL, JSP, JSF, Facelets and CDI. With Java EE 7 EL finally got it's own JSR. Just some of the crucial changes in EL 3 include a standalone API, support for lambda expressions, static field and method access, improved collection processing and much, much more.

Published in: Technology

What’s New with Expression Language in Java EE 7

  1. 1. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1 What’s New with Expression Language in Java EE 7 Reza Rahman Java EE/GlassFish Evangelist Reza.Rahman@Oracle.com @reza_rahman
  2. 2. 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
  3. 3. 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
  4. 4. 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”);
  5. 5. 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”);
  6. 6. 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 repository elm.definBean(“name”, new Name(“Peter”)); // Add a map to bean look-up elm.addBeanNameResolver(new BeanNameResolver() { @Override public Object getBean(String name) { return myDB.get(name); } });
  7. 7. 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”);
  8. 8. 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”);
  9. 9. 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”);
  10. 10. 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);
  11. 11. 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”);
  12. 12. 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)");
  13. 13. 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
  14. 14. 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
  15. 15. 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)  11 fact = n->n==0? 1: n*fact(n-1); fact(5)  120
  16. 16. 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()”);
  17. 17. 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]}
  18. 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
  19. 19. 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()
  20. 20. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20 Expression Language 3 Collection 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()
  21. 21. 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
  22. 22. 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/
  23. 23. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23

×