Jabel: All the syntax of Java 12, targeted to Java 8

Share
  • September 3, 2019

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


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.

SEE ALSO: Migrating big data workloads to Azure HDInsight – Smoothing the path to the cloud with a plan

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, var declarations, 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

SEE ALSO: The ethics of AI – dealing with difficult choices in a non-binary world

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!

The post Jabel: All the syntax of Java 12, targeted to Java 8 appeared first on JAXenter.

Source : JAXenter