Parsing and Translating Java 8 lambda expressions

c# expression-trees java java-8 lambda


In C# you can enclose a lambda expression in an expression tree object and then possibly parse it. I was wondering if this is also possible in Java?

What I'm looking for is doing something like this:

.where(b -> b.getIban() == "SomeIban")
.and(b -> b.getAuthor() == "SomeAuthor"); //etc.

And then the BooksRepository should somehow translate that query to the following RESTful API request based on the predicates specified as lambdas:


and then return the result to the client. Any idea if this is even possible in Java?

9/24/2014 5:09:33 PM

Accepted Answer

Yes, it's possible. I made a library that does exactly that: JaQue

12/21/2014 8:39:22 AM

Popular Answer

I'm the author of Jinq. The automatic conversions of lambdas into expression trees that can be read at runtime is a C# only feature. That feature doesn't really have any general-purpose use for normal programmers. It's basically only useful for implementing LINQ, so language designers haven't been keen on adding that feature to Java or other languages. There has been talk of adding some sort of compiler plugin framework for Java that would allow you to add the expression tree feature to Java yourself, but I'm not too familiar with progress on that project.

Jinq contains a bytecode analysis framework that can build simple expression trees from the bytecode of a lambda at runtime. It is based on symbolic execution and ASM. It can be used to convert lambdas into expression trees, just like C#, but it's pretty heavy weight procedure with a lot of hoops to jump through, so it might be overkill for what you want to do. You'd be swatting a fly with a hammer, essentially.

There are languages other than Java that run on the JVM that have better support for the metaprogramming that you want to do. So you might also want to consider looking at Groovy or Scala for that.

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow