Jabel: All the syntax of Java 12, targeted to Java 8
Not everyone migrates to the latest Java release right away. In fact, Java 8 is still dominating, especially in the enterprise and in open source libraries. So, what do you do when you want to use Java 12+ syntax, but all of your users are stuck in the past using Java 8? This Javac 12 hack allows you to target Java 8, but use Java 12+ syntax.
Java 8 may be headed towards the retirement home, but this version still dominates the ecosystem even as it went through the End of Public Updates process for legacy releases. Many open source libraries still use it as their target language, despite the new language features introduced in later versions.
Even when developers wish to use later versions, upgrading at work is time-consuming, and thus many developers still use older versions until they are forced to upgrade out of necessity. This locks them out of using the latest features, even if they have the perfect idea.
We’re all the way up to Java SE 12 by now, which includes game-changing features such as an experimental low-pause-time garbage collector, and a microbenchmark suite. So, what do you do when you want to use Java 12+ syntax, but all your users are still hanging onto 8? Jabel unlocks the magic formula, allowing you to use Java 12+ syntax, all while targeting Java 8.
Javac 12 hacked
the PoC was much easier than I thought…
It’ll surely cause some 🍑🔥 but YOLO😅https://t.co/IXohVzGHoY
Jabel – a hack for Javac 12 that makes it emit Java 8 bytecode.
Vars, Switch Expressions, and other Java 9+ language(!) features without upgrading your Java requirement 🎉 https://t.co/I5nBabc1f2
— Sergei Egorov (@bsideup) August 31, 2019
This open source project by Sergei Egorov is a useful little hack that makes Javac 12 emit Java 8 bytecode. With it, you can use features introduced in later versions such as Vars and Switch Expressions, without even migrating.
How does it work? Take a peek behind the curtain. From the GitHub README:
Although Jabel is an annotation processor, it does not run any processing, but instruments the java compiler classes and makes it treat some new Java 9+ languages features as they were supported in Java 8.
The result is a valid Java 8 bytecode for your switch expressions,
vardeclarations, and other features unavaliable in Java 8.
The JVM has evolved a lot for the past years. However, most language features that were added are simply a syntatic sugar. They do not require new bytecode, hence can be compiled to the Java 8.
Jabel performs a few tricks by making the compiler think that certain features were developed for v8. It removes the checks that would ordinarily catch these features and mark them as invalid.
Impressively, the entire project is very small, weighing in at just under 150 lines of code.
Setting it up
Check out the hack for yourself. The plugin is distributed with Jitpack.
The repo on GitHub has instructions for both Maven and Gradle. Compile the project, see what it can do, and let the developer know how it works!