Releases

Support for Java 9 in IntelliJ IDEA 2017.2

As Java 9 continues to evolve, so does IntelliJ IDEA’s support for it. This screencast shows the new features to support Java 9 development in the upcoming IntelliJ IDEA 2017.2 release.

 

In the rest of this blog post we’ll break down what’s in the video and talk about each feature.

As you’d expect, a lot of the improvements are to support the Java Platform Module System. One of the new features to help developers getting up to speed with Jigsaw and Java modularity is the new Java Module Diagram. This diagram shows the modules you’ve defined in your project in green; any automatic modules (those modules that are dependencies on traditional jar files) in blue; and any JDK modules in yellow.

Java Module Diagram

You’ll notice there are two different types of lines in the diagram, the thick blue lines show “requires transitive“, the thinner black lines are standard “requires” dependencies.

These diagrams give a clear picture of which modules have been defined and which modules they depend upon, and can help you either to create a clean separation when creating a modular system, or simply visualize what’s going on with the new Java Platform Module System.

If you are working with Java 9 modules, you’ll see code completion and generation has been improved for the module-info.java file. For a start, the generated module name more closely matches the IntelliJ IDEA module name.

Error in open module

Errors and warnings and their suggested fixes have also been improved. For example, if you try to use opens in a module that is already open , the error is clear and you can opt to either remove the opens statement or change the module so it’s no longer open.

Open module fixes

Duplicate opens or provides statements are detected and can be deleted or merged.

Duplicate opens statement

You can also use find usages on a module name to locate other places that use this module, including JDK modules that use it.

Find module usages

And, of course, you can refactor module-info files, for example using rename.

Project Jigsaw has a wider impact than just allowing you to create your own modules.  Modularity impacts the visibility of packages, so classes that were previously accessible may not be any more. This even includes reflection. IntelliJ IDEA’s inspections can help locate reflection code that tries to access modules that are not on the module path, and offers suggestions to fix the problem.

Reflection warnings

There are a few new inspections to help migrate code to Java 9. There’s a section under the Java language level migration aids inspections for Java 9.

New inspections

The first inspection highlights module-info files that require automatic modules – automatic modules are useful during migration to Java 9, but over time code should be migrated to use true modules. By default, this only highlights transitive dependencies, as this means your code is exposing automatic modules to other modules.

Using unmodifiableSet

The second inspection points out places where we can use the new Collections Factory Methods. For example, if we’re creating an unmodifiableSet, this verbose code can be enormously simplified in Java 9. We can also convert unmodifiableList to the new syntax too.

Using Set.of

There’s one final inspection to help us make use of new Java 9 features. If your code has while loops that wait for a particular value to change, they can be updated to add the new onSpinWait hint, which allows the runtime to potentially optimize this operation.

Suggestion to add onSpinWait

Finally, IntelliJ IDEA is also ready for the updates to Javadoc in Java 9. Generated Javadoc is now searchable, letting developers search for classes, methods or keywords in the documentation. To make the most use of this, you may want to add an index tag to your documentation to provide keywords to the search.

image description