Advanced Support for Java 9 Modules in IntelliJ IDEA 2018.1
Since Java 9 was released, many conferences have hosted discussions around its new features and use cases. Out team couldn’t miss this opportunity to collect valuable community feedback on what Java 9 brings us. This feedback has inspired us to improve Java 9 support in IntelliJ IDEA. Specifically, navigation via gutter icons, as well as new warnings and related quick-fixes are coming to IntelliJ IDEA 2018.1. Let’s take a closer look at what’s already available.
As you may know, starting with Java 7, you can use Java services for loose coupling within a modular application. The API for that is provided by the java.util.ServiceLoader class. But in Java 9, modules are configured differently; specifically, uses and provides clauses should be declared in the module declaration file (the module-info.java file). When using Java 9, before you load a service with ServiceLoader to access service implementations, you need to make sure that the uses clause is added in the module-info.java file. It’s also worth mentioning that when the service class provided as an argument of ServiceLoader.load() isn’t declared in the module-info.java file, this leads to a runtime error.
IntelliJ IDEA now detects such problems. The IDE checks that the service loaded by ServiceLoader is declared in the module-info.java file. It then offers a quick-fix to add a missing statement to the module-info.java file.
For services declared in the module-info.java file, the IDE now provides gutter icons for easy navigation. Click the gutter icon next to the service implementations to navigate to the provides clause.
When the service is used with ServiceLoader, there is a gutter icon next to its usage. By clicking this gutter icon, you can navigate to uses clause in the module-info.java file.
IntelliJ IDEA also warns you if there is an unresolved class in the module-info.java file. In addition, when the module-info.java file refers to a non-existing class, the IDE suggests creating the missing class. It provides a popup to select the type of a class to create and a source root where to save this new class (in case there are more than one source roots in your module).
This new feature also works for missing exported packages as well. The IDE creates the package with the class in the required directory. It offers you to create a class in that package because you can’t export an empty package in Java 9.
Download the latest IntelliJ IDEA EAP build, try these new features right away, and tell us what you think! Please share your impressions in the comments below, tweet at us, or send bug reports to our issue tracker.