Lambda expressions backported to Java 7, 6, and 5

Original author: Esko Luontola
  • Transfer

Want to use lambda expressions but are forced to use a stable JRE? This is now possible with the Retrolambda utility , which converts Java 8 bytecode to Java 7, 6, and 5 suitable for runtimes, allowing the use of lambda expressions and method references on these platforms. Of course, you will have to abandon the improved Java 8 Collections API, but, fortunately, there are many alternative libraries that will be easier to work with thanks to lambda expressions.

Under the hood

A couple of days ago in a cafe, the idea came to me to look for someone who was already dealing with this problem, but after a couple of conversations with the wall , he did everything on his own over the weekend.

The initial blunt plan for copying classes from OpenJDK failed ( LambdaMetafactory depends on some private classes and required modification), but in the end I found a way to avoid unnecessary dependencies on runtime.

Retrolambda uses the Java agent to find the bytecode dynamically generated by LambdaMetafactory, save it as a .class file, and replace instructionsinvokedynamicto explicitly initialize these classes. The library also changes the visibility modifier of some synthetic methods to package, so the bytecode can access them without using the MethodHandle .

Also popular now: