Setting Java 8, 9, 10, etc

We needed to do some refactoring for Java 10 support, so you’ll find the old way below under “pre 3.5.14” What you want to do nowadays is to pick a postprocessor that turns the AST into an AST for the Java version you want, and a validator that will find problems in the AST for you. Luckily, that’s all hidden behind a simple setting in ParserConfiguration:

Okay… Let’s do an import static:

Well, that was quick. There are some pseudo-language levels available (from 3.5.15 on):

  • POPULAR gives you the most common Java version at the time of release.
  • LATEST gives you the latest stuff that JavaParser supports.
  • RAW skips postprocessing and validation for a bit of a speed boost.

Pre 3.5.14 (old stuff)

When support for parsing Java 9 files was added in version 3.1.4, we didn’t turn it on by default. We set the default language level to 8, with the idea to set it to 9 around the time JDK 9 comes out. So how do you get that delicious module support? By changing the validations done to the AST. Where are these validations set? In  ParserConfiguration.

Easy mode

The easy API uses static methods, so it is logical that the configuration used is static too:

or, as a one-liner:

Pro mode

The pro API uses an instance of  JavaParser, and the configuration goes into the constructor:

or, as a one-liner:

This works for every major Java version:

Be the first to comment

Leave a Reply