days
-6
-5
hours
0
-4
minutes
-2
-6
seconds
0
-2
search
Faster than upgrading

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

Sarah Schlothauer
java
© Shutterstock / Morphart Creation

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


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

    In a cloud native world enamored with microservices and serverless, meet Quarkus – Java’s brilliant response to technologies like Node.js, Python and Go that had proven quicker, smaller and arguably more nimble. Download your free beginner's tutorial written by JAX London speaker Alex Soto.

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!

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat. She is also the editor for Conditio Humana, an online magazine about ethics, AI, and technology.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of