Parsing and Translating Java 8 lambda expressions

c# expression-trees java java-8 lambda

Question

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:

BooksRepository.getAll()
.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:

GET http://mylibrary.com/books?Iban=SomeIban&Author=SomeAuthor

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

Accepted Answer

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


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.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why