📜 ⬆️ ⬇️

Lambda expressions are backported to Java 7, 6 and 5


Want to use lambda expressions, but have to use a stable JRE? This is now possible with the Retrolambda utility, which converts Java 8 bytecode into Java 7, 6 and 5 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, I had the idea to look for someone who was already working on this problem, but after a couple of conversations with the wall , I did everything on my own over the weekend.

The original plan of stupid copying classes from OpenJDK failed ( LambdaMetafactory depends on some private classes and required modification), but ultimately I found a way to avoid unnecessary dependencies on runtime.
')
Retrolambda uses Java agent to search for bytecode, which is dynamically generated by the LambdaMetafactory, save it as a .class file, and replace invokedynamic instructions with explicit initialization of these classes. The library also changes the visibility modifier of some synthetic methods to package, so the bytecode can access them without using MethodHandle .

Source: https://habr.com/ru/post/187596/


All Articles