45
votes

Reading about what kind of bytecode Java 8 produces from lambdas, it came to my mind the time when Java 5 was released. Back then there was Retroweaver and other tools for converting bytecode compiled with JDK 5 to run on JRE 1.4.

Has anybody yet created such a backporting tool for Java 8 lambdas? It would let Java developers start using lambdas already today on production-quality Java 7 JREs, without having to wait 6-12 months for Java 8's GA release.

Here is my analysis of why such as backporter should be implementable relatively easily:

Java 8 lambdas don't seem to use any JVM features that Java 7 wouldn't have (e.g. invokedynamic), and the java.lang.invoke.LambdaMetafactory class and its dependencies look like pure Java, so it should be possible to implement them in a 3rd-party library. Thus bytecode compiled with JDK 8 could be made to run on JRE 7 by adding a 3rd-party library with a copy of LambdaMetafactory (under a different package) and by transforming the bytecode to use that metafactory instead. Maybe also generate some synthetic classes and methods to bypass accessibility checks, as java.lang.invoke.MagicLambdaImpl seems to imply. Or then generate anonymous inner classes for all lambdas, like some of the first lambda-enabled Early Access JDKs did.

1
In case nobody reports such a thing soon, I might write one. Here is the (currently empty) github repository: github.com/orfjackal/retrolambda – Esko Luontola
Hmmm... this would meet a real demand. If you can get it to work correctly and with as few limitations as possible, I think it's a winner. – Radu Murzea
I experimented a bit on my own and found out that with bytecode transformation it is even possible to run the classes compiled for Java 8 directly with a Java 7 JVM with an appropriate javaagent. The JVM will accept the classes even when they have an unsupported format as long as the instrumentation via registered class file tranformers results in a known, valid format. I did not implement all features (yet) but I think it’s a promising approach as keeping the deployed classes unmodified means they might still benefit from future Java 8 versions. – Holger

1 Answers

44
votes

There is now Retrolambda for converting Java 8 bytecode, which uses lambda expressions and method references, to work on Java 7, 6 or 5. (Java 1.4 gave verify errors; did not investigate further.)