What’s New with Expression Language in Java EE 7

23
What’s New with Expression Language in Java EE 7 Reza Rahman Java EE/GlassFish Evangelist [email protected] @reza_rahman

description

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.

Transcript of What’s New with Expression Language in Java EE 7

Page 1: What’s New with Expression Language in Java EE 7

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

Page 2: What’s New with Expression Language in Java EE 7

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

Page 3: What’s New with Expression Language in Java EE 7

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

Page 4: What’s New with Expression Language in Java EE 7

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”);

Page 5: What’s New with Expression Language in Java EE 7

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”);

Page 6: What’s New with Expression Language in Java EE 7

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); }});

Page 7: What’s New with Expression Language in Java EE 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”);

Page 8: What’s New with Expression Language in Java EE 7

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”);

Page 9: What’s New with Expression Language in Java EE 7

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”);

Page 10: What’s New with Expression Language in Java EE 7

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);

Page 11: What’s New with Expression Language in Java EE 7

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”);

Page 12: What’s New with Expression Language in Java EE 7

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)");

Page 13: What’s New with Expression Language in Java EE 7

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

Page 14: What’s New with Expression Language in Java EE 7

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

Page 15: What’s New with Expression Language in Java EE 7

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

Page 16: What’s New with Expression Language in Java EE 7

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()”);

Page 17: What’s New with Expression Language in Java EE 7

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]}

Page 18: What’s New with Expression Language in Java EE 7

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

Page 19: What’s New with Expression Language in Java EE 7

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()

Page 20: What’s New with Expression Language in Java EE 7

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()

Page 21: What’s New with Expression Language in Java EE 7

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

Page 22: What’s New with Expression Language in Java EE 7

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/

Page 23: What’s New with Expression Language in Java EE 7

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23