Support for Java 9 in IntelliJ IDEA
Support for Java 9 has come to IntelliJ IDEA! Check out this post to find out some of the Java 9-specific features that have been included.
Join the DZone community and get the full member experience.Join For Free
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.
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.
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.
provides statements are detected and can be deleted or merged.
You can also use find usages on a module name to locate other places that use this module, including JDK modules that use it.
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 anymore. 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.
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.
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.
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.
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 optimise this operation.
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.
Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.