Import Control
Import control annotation to use for lightweight module system in Java.
Motivation
Java previous to JDK 9 has a visibility concept that does not support modules very well. If modules are structured into various packages, and only dedicated packages should be exported to other modules, the visibilty modifiers do not fit at all. All public classes can be imported to other modules.
With JDK 9 JPMS was introduced to solve this issue. Using JPMS in a project is not an easy task and sometimes even not possible due to runtime restrictions (see jpms-negative-benefits). This is where Import Control comes to rescue. It fully supports the need for import control between modules but with no runtime impact nor restrictions. Packages of modules that shall be exported to other modules can be annotated with @ExportPackage
. Your IDE can control whether your imports are correct or not.
How to use
In your projects modules, annotate the packages of modules that should be exported to other modules with the annotation com.github.frimtec.libraries.importcontrol.api.ExportPackage
. To annotate a package create a package-info.java
file inside the package folder with the following content:
@ExportPackage
package org.organisation.project.module;
IDE-Integration
IntelliJ-IDEA
Install the plugin IDEA Import Control to add a new code inspection rule that marks all undesirable imports as errors.
Add dependency
To use import-control in your project you can add the dependency from maven central to your software project management tool: In Maven just add the following dependency to your pom.xml:
<dependency>
<groupId>com.github.frimtec</groupId>
<artifactId>import-control-api</artifactId>
<version>1.1.1</version>
</dependency>