Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has anybody yet backported Lambda Expressions to Java 7?

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.

like image 402
Esko Luontola Avatar asked Jul 19 '13 22:07

Esko Luontola


People also ask

Does Java 7 have lambda expressions?

From the Retrolambda documentation: Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .

Can I run Java 8 code on Java 7?

Binary CompatibilityJava SE 8 is binary-compatible with Java SE 7 except for the incompatibilities listed below. Except for the noted incompatibilities, class files built with the Java SE 7 compiler will run correctly in Java SE 8.

Does Java 8 support lambda?

Java 8 provide support for lambda expressions only with functional interfaces. Functional Interface is also a new feature introduced in Java 8. Any Interface with single abstract method is called Functional Interface.


1 Answers

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.)

like image 129
Esko Luontola Avatar answered Sep 28 '22 05:09

Esko Luontola