adam bien's blog

The Hidden JavaEE 7 Gem - Lambdas With JDK 1.7 📎

EL 3.0 (Expression Language 3.0) JSR-341 is part of JSF, JSP and so a JavaEE 7 and comes with amazing capabilities. You can pass objects to the ELProcessor to access the properties, define collection literals or perform computations:

import java.util.Map;
import java.util.Set;
import javax.el.ELProcessor;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;

public class ELTest {

    private ELProcessor cut;

    @Before
    public void init() {
        this.cut = new ELProcessor();
    }

    @Test
    public void formula() {
        Long result = (Long) this.cut.eval("2*2");
        assertThat(result, is(4l));
    }

    @Test
    public void bean() {
        Workshop workshop = new Workshop("javaee airhacks");
        this.cut.defineBean("workshop", workshop);
        String title = (String) this.cut.eval("workshop.title");
        assertThat(title, is(workshop.getTitle()));
    }

    @Test
    public void listLiteral() {
        String listLiteral = "{1,2,3}";
        Set list = (Set) this.cut.eval(listLiteral);
        assertFalse(list.isEmpty());
        System.out.println("List: " + list);
    }

    @Test
    public void mapLiteral() {
        String listLiteral = "{\"one\":1,\"two\":2,\"three\":3}";
        Map map = (Map) this.cut.eval(listLiteral);
        assertFalse(map.isEmpty());
        System.out.println("Map: " + map);
    }
    
}

Also EL 3.0 allows you to declare and execute lambdas …on Java 7:


    @Test
    public void lambda(){
        Object result = this.cut.eval("[1,2,3,4,5,6,7,8].stream().
							filter(i->i%2 == 0).map(i->i*10).toList()");
        assertNotNull(result);
        System.out.println("Result: " + result);
        result = this.cut.eval("[1,5,3,7,4,2,8].stream().sorted((i,j)->j-i).toList()");
        System.out.println("Result: " + result);
    }


ELProcessor can be started outside the application server within JavaSE environment. You only need a single maven dependency:

<dependency>
	<groupId>org.glassfish</groupId>
	<artifactId>javax.el</artifactId>
	<version>3.0.0</version>
</dependency>

The code above is an example used during the airhacks.com.

See you at Java EE Workshops at MUC Airport!