Skip to content
fblanPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

modular

Modules for Java packages, checked when your code compiles.

modular lets you split one Java code base into modules, without JPMS module-info.java and without splitting it into several Maven or Gradle projects. You declare a module with an annotation on a package-info.java, saying which packages it exports and which modules it requires. An annotation processor then rejects, at compile time, any code that crosses a module boundary it shouldn't.

@Module(name = "engine", exports = {"."}, requires = {"rules"})
package com.example.engine;

import org.asymetrik.modular.annotations.Module;
[ERROR] FizzBuzzApp.java:[26,8] Requires violation: 'app' (com.example.app) uses 'com.example.rules.Rules'
        from module 'rules' (com.example.rules) without requiring it. Add "rules" to the requires of module 'app' ...

It also lets each module state its requirements, as sentences in EARS format, and link unit tests to them:

@Module(name = "engine", exports = {"."}, requires = {"rules"})
@Req(id = "ENG-1", value = "When no rule matches a number, the engine shall say the number itself.")
package com.example.engine;
@Test
@TestReq("ENG-1")
void saysTheNumberWhenNoRuleMatches() { ... }

Contents

Getting started

Requires Java 21 or later. Artifacts are on Maven Central, under the group org.asymetrik.modular.

Artifact What it contains Needed at
annotations @Module, @ExternalModule compile time
processor ModuleAnnotationProcessor: checks the module rules compile time
specification @Req, @TestReq, and RequirementProcessor: checks the requirements compile time
verification ModularVerifier: the same module rules, checked from a test with ArchUnit tests (optional)
report ModularReport: an HTML report on the modules tests (optional)

Maven

<properties>
    <modular.version>0.1.2</modular.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.asymetrik.modular</groupId>
        <artifactId>annotations</artifactId>
        <version>${modular.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- Optional: requirements (@Req, @TestReq) -->
    <dependency>
        <groupId>org.asymetrik.modular</groupId>
        <artifactId>specification</artifactId>
        <version>${modular.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <!-- Since JDK 23, javac no longer finds processors on the class path by itself. -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.asymetrik.modular</groupId>
                        <artifactId>processor</artifactId>
                        <version>${modular.version}</version>
                    </path>
                    <!-- Optional: requirements -->
                    <path>
                        <groupId>org.asymetrik.modular</groupId>
                        <artifactId>specification</artifactId>
                        <version>${modular.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

The annotations are only read at compile time, so provided keeps them out of your runtime class path.

Gradle

dependencies {
    compileOnly("org.asymetrik.modular:annotations:0.1.2")
    annotationProcessor("org.asymetrik.modular:processor:0.1.2")

    // Optional: requirements
    compileOnly("org.asymetrik.modular:specification:0.1.2")
    testCompileOnly("org.asymetrik.modular:specification:0.1.2")
    annotationProcessor("org.asymetrik.modular:specification:0.1.2")
    testAnnotationProcessor("org.asymetrik.modular:specification:0.1.2")
}

Try it on the sample

sample-fizzbuzz is a small application split into three modules, with requirements and tests. Its README lists experiments that trigger each kind of error.

Declaring modules

@Module

A module is a package annotated with @Module in its package-info.java. It contains that package and all its sub-packages.

@Module(
    name = "billing",                                   // unique name, used in other modules' requires
    exports = {".", "com.example.billing.api.*"},       // packages other modules may use
    requires = {"customers", "java.sql", "slf4j"}       // modules this module may use
)
package com.example.billing;

import org.asymetrik.modular.annotations.Module;
Attribute Meaning
name The module's name. Must be unique: other modules reference it in requires.
exports Packages that other modules may use. "." is the module's own package (not its sub-packages). A full package name exports exactly that package. "com.example.billing.api.*" exports the sub-packages of com.example.billing.api. {} exports nothing.
requires Names of the modules this module may use: other @Modules, @ExternalModules, or JPMS module names. Defaults to {}.

The rules

  1. No nested modules. A module's package may not contain another @Module package, at any depth. Modules are peers: com.example.billing and com.example.customers, not com.example.billing.customers.
  2. Exports. A class may only use another module's classes from packages that module exports.
  3. Requires. A class may only use another module if its own module lists that module in requires. requires is not transitive: if app requires engine and engine requires rules, app must also require rules to use it.
  4. Only java.* is free. Outside your modules, only java and java.* packages can be used without being required. Anything else (javax.*, jakarta.*, libraries, or your own packages without @Module) must belong to a module that is required, as described below.

The processor finds dependencies in signatures (fields, parameters, return types, supertypes), in method bodies (local variables, calls, static imports, fully qualified names), in import declarations (including wildcard imports), and in string literals that name a class. Body scanning can be turned off with the javac option -Aorg.asymetrik.modular.treeScan=false, for faster builds; signatures are always checked.

External dependencies: JPMS names and @ExternalModule

A class outside java.* and outside your modules belongs to the first of:

  1. An @ExternalModule that covers its package (see below).
  2. Its JPMS module:
    • for the JDK and the module path, its named module: java.desktop for javax.swing, java.sql for javax.sql, jdk.compiler for com.sun.source;
    • for a jar on the class path, the name JPMS would give it: its module-info, else its Automatic-Module-Name, else a name derived from the file name. For example jakarta.validation for jakarta.validation-api.jar.

So in most cases, requiring the JPMS name is enough:

@Module(name = "app", exports = {}, requires = {"engine", "java.desktop", "jakarta.validation"})

To group packages under a name of your own, or for packages with no JPMS name, declare a virtual module with @ExternalModule, on any package-info.java:

@ExternalModule(name = "slf4j", packages = {"org.slf4j"})
@ExternalModule(name = "inject", packages = {"jakarta.inject"})
package com.example.modules;

import org.asymetrik.modular.annotations.ExternalModule;
  • A listed package also covers its sub-packages: "org.slf4j" covers org.slf4j.event.
  • Everything in a virtual module is exported.
  • When several virtual modules cover a package, the most specific one wins. A real @Module always wins.
  • Virtual module names share the namespace of module names: they must be unique.

A class with none of these owners, such as one of your own packages without @Module, is an Unmanaged dependency.

Requirements

The specification artifact adds @Req: requirements of a module, written next to its @Module.

@Req

@Module(name = "engine", exports = {"."}, requires = {"rules"})
@Req(id = "ENG-1", value = "When no rule matches a number, the engine shall say the number itself.")
@Req(id = "ENG-2", value = "When several rules match a number, the engine shall concatenate their words in rule order.")
@Req("While the upper bound is below 1, the engine shall say nothing.")
package com.example.engine;

import org.asymetrik.modular.annotations.Module;
import org.asymetrik.modular.specification.Req;

Each requirement is one sentence in EARS format, and its subject is the module:

Pattern Form
Ubiquitous The engine shall <response>.
Event-driven When <trigger>, the engine shall <response>.
State-driven While <state>, the engine shall <response>.
Unwanted behaviour If <condition>, then the engine shall <response>.
Optional feature Where <feature is included>, the engine shall <response>.
Combined While <state>, when <trigger>, the engine shall <response>.

RequirementProcessor checks that @Req is on a @Module package, that each sentence follows a pattern, that its subject is the module (ignoring case), and that ids are unique. Ids are optional; set one to reference the requirement from tests.

@TestReq

class FizzBuzzTest {

    @Test
    @TestReq("ENG-1")
    void saysTheNumberWhenNoRuleMatches() { ... }

    @Test
    @TestReq({"ENG-2", "RUL-1"})
    void concatenatesTheWordsInRuleOrder() { ... }
}

@TestReq goes on a test method, or on a test class to cover all its tests. When the main code compiles, the processor writes the requirements with an id to META-INF/modular/requirements.properties. When the tests compile, it reads that file from the class path:

  • an id that matches no @Req is an error, on the test;
  • a requirement with an id that no test covers is a warning: Requirement 'APP-1' of module 'app' has no test: "...". Add @TestReq("APP-1") to a test that checks it.

These checks only run in compilations that contain at least one @TestReq.

Checking in tests with ModularVerifier

verification checks the same module rules on compiled classes with ArchUnit, from a test. It also reports names in requires that match no module, which the processor cannot do reliably.

<dependency>
    <groupId>org.asymetrik.modular</groupId>
    <artifactId>verification</artifactId>
    <version>${modular.version}</version>
    <scope>test</scope>
</dependency>
@Test
void modulesAreRespected() {
    ModularVerifier.Result result = new ModularVerifier("com.example").verify();

    assertThat(result).isInstanceOf(ModularVerifier.Valid.class);
}

verify() returns Valid, or the first violation found: NestedModuleViolation, DuplicateModuleNameViolation, UnknownRequiredModuleViolation, RequiresViolation, ExportViolation or UnmanagedDependencyViolation. Each violation has a formatError() method with a readable message.

Every violation, and the modules themselves: ModuleAnalysis

ModularVerifier stops at the first violation. ModuleAnalysis gives all of them, and what it learned about the modules on the way:

ModuleAnalysis analysis = ModuleAnalysis.of("com.example");

analysis.violations();         // every violation: nested modules, then names, then dependencies
analysis.modules();            // name, package, exports, requires, and the types of each module
analysis.externalModules();    // the @ExternalModule declarations
analysis.dependencies();       // what each module really uses, with the classes behind it
analysis.unassignedClasses();  // scanned classes that are in no module
  • Types: each top-level type has its package, whether that package is exported, its number of methods and public methods, the size of its class files, and its lines of code, with those of each of its methods. Lines of code are the lines that hold code, read from the line numbers of the class files: comments and blank lines are not counted, and a lambda counts with the method that holds it. A method without line numbers, such as one that a framework adds, is left out; a class compiled without line numbers (-g:none) has unknown lines. Nested and anonymous classes are counted with their top-level type.

  • Dependencies: one per module and target. The target is a module, a virtual module, a JPMS module, or an unmanaged package. A dependency is listed whether the rules allow it or not. Uses of java.base are left out; a use of another JDK module, such as java.net.http, is a dependency on that JPMS module. It breaks no rule: java.* can be used without requires.

  • Test classes: ModuleAnalysis.of(ClassScope.WITHOUT_TESTS, List.of("com.example")) leaves out the classes of test output directories.

  • Generated code: a framework that generates classes in your packages, or weaves code into your classes, makes them use modules that no requires lists. The processor reads sources, so it never sees that code; the analysis reads bytecode, so it does. Leave it out with Exclusions:

    ModuleAnalysis.of(ClassScope.WITHOUT_TESTS, List.of("com.example"),
        new Exclusions(List.of("**$$*", "**RepositoryImpl"), List.of("io.example.framework.Link")));

    The first list names classes to leave out entirely: they are not measured, not analysed, and not "in no module" (a nested class goes with its top-level class). The second names used classes to ignore: a use of one of them is no dependency and no violation. In a pattern, * matches any characters except ., ** any characters, and the rest is literal; it matches the fully qualified name, nested classes written with $.

To fail a test with every message at once:

assertThat(ModuleAnalysis.of("com.example").violations())
    .extracting(ModularVerifier.Invalid::formatError)
    .isEmpty();

The analysis reads bytecode, so it cannot see what only exists in source: a constant of another module that the compiler inlined, or an annotation with source retention.

Module report

The rules tell you when a boundary is crossed. The report shows you the modules themselves, to check that they are the ones you intended: one HTML page with the size of each module, the dependencies between them, a diagram, and a list of findings.

The module report of the FizzBuzz sample: summary, findings and diagram

<dependency>
    <groupId>org.asymetrik.modular</groupId>
    <artifactId>report</artifactId>
    <version>${modular.version}</version>
    <scope>test</scope>
</dependency>

Write it from a test, which has the class path of your application:

@Test
void writesTheModuleReport() {
    ModularReport.of("com.example").writeTo(Path.of("target/modular-report"));
}

Then open target/modular-report/index.html in a browser. It is a single file that needs no network access, so it can be kept as a build artifact or sent as is. Test classes are left out; to choose the classes yourself, pass a ModuleAnalysis to ModularReport.of.

Without a test, run it with the classes of your application on the class path:

java -cp <class path> org.asymetrik.modular.report.ModularReport target/modular-report com.example

The command line takes the exclusions of the analysis (see "Generated code" above), each option repeated as needed. The page then says what was left out, under the scanned packages.

java -cp <class path> org.asymetrik.modular.report.ModularReport \
    '--exclude-class=**$$*' '--exclude-use=io.example.framework.Link' target/modular-report com.example

What the report shows

  • Summary: whether the module rules hold, and the number of modules, types, dependencies and findings.
  • Findings: what to look at, most serious first (see below).
  • Diagram: a box per module, an arrow from a module to each module it uses. A module sits below the modules that use it, so arrows point down; the arrows of a cycle are red. A box is taller when the module has more types, an arrow is wider when there are more uses. Select a module to highlight its arrows. A module with findings takes the color of the most serious one: red for an error, amber for a warning, blue for a hint.
  • Dependency matrix: the number of uses of each module by each other module.
  • Modules: for each module, its exports and requires, its size (types, public methods, lines of code), what it uses and who uses it, with the classes behind each dependency, and its types. A module with findings is marked in the table, with their number, and its section lists them, linked to the Findings section. The modules come by their most serious finding, then by lines of code, the largest first, and a box filters them by name.
  • Outside the modules: the virtual modules, JPMS modules and unmanaged packages that the modules use.
Finding Level Meaning
Violation error A module rule is broken: the same violations as ModuleAnalysis.violations().
Cycle warning Modules that use each other, directly or not. No rule forbids it, so only the report shows it.
Classes in no module warning Scanned classes that are in no @Module and in no @ExternalModule.
Large module warning A module with more than 50 types: it probably holds several parts of the domain.
requires not seen in use hint A name in requires that the module's classes do not use.
Exported package not used hint A package in exports that no other module uses.
Large type hint A type with more than 20 public methods: it probably does several jobs.
Large method hint A method with more than 75 lines of code, SonarQube's default limit: it probably does several things.
Leaky API hint A module uses exported types of another module that use a third one, and doesn't require that third one: it can't use what it receives without a new requires. From 0.1.3, a use that is only an annotation, such as @Embeddable on an exported type or an enum given as an annotation's value, doesn't count: the users of the type never have to know it.
Borrowed exception hint A module creates exceptions of another module, so its errors depend on how that module handles them. Catching them is fine.
Exported helper hint An exported class of static helpers (static members only, and no method returning a type of its own module, which would make it a factory) that another module uses: shared code that belongs to neither.

A hint can be a false alarm. The report reads bytecode: it does not see a constant that the compiler inlined or an annotation with source retention, and an exported package can be an entry point, or be used by reflection. A large type or method, an exported helper or a shared exception can also be a choice.

The findings are also available in Java, for example to keep a code base free of cycles:

assertThat(ModularReport.of("com.example").findings())
    .filteredOn(finding -> finding.kind() == Finding.Kind.CYCLE)
    .isEmpty();

The report itself never fails a build.

Errors and how to fix them

Error Cause Fix
Module hierarchy violation A @Module package is inside another one. Move the inner module to a sibling package, or remove its @Module.
Duplicate module name Two modules (or virtual modules) share a name. Rename one of them.
Requires violation A class uses a module that its module does not require. Add the name given in the message to requires, or stop using the class.
Export violation A class uses a package that its module does not export. Use an exported class instead, or export the package from the providing module. When reported on a wildcard import, replace the wildcard with explicit imports of exported types.
Unmanaged dependency A class uses a package outside java.* that belongs to no module, no virtual module, and has no JPMS name. Declare an @ExternalModule covering the package and require it, or give your own package a @Module.
Unknown required module (ModularVerifier) A name in requires matches nothing. Fix the name, or remove it.
Requirement … is not an EARS sentence A @Req does not follow an EARS pattern. Rewrite it with one of the forms listed in the message.
Requirement … has '…' as subject The sentence names another system. Write "the <module name> shall …".
Requirement on package …, which is not a @Module @Req without @Module. Move it to the module's package-info.java.
Duplicate requirement id Two @Reqs share an id. Give one a new id.
Unknown requirement id … in @TestReq The test references an id that no @Req has. Fix the id, or add the requirement.
Requirement … has no test (warning) No @TestReq references the requirement. Add @TestReq("<id>") to a test that checks it.

IDE support

The processors run in any build that runs javac with them: Maven, Gradle, and IntelliJ IDEA after importing the Maven or Gradle project.

The processors only see what is being compiled. A full build (mvn clean compile, or Build → Rebuild Project) checks everything. An incremental build in the IDE that recompiles a single class may miss a violation, because the package-info.java declaring its module is not recompiled with it. It may also show a coverage warning for requirements that other, not recompiled, tests cover. Run a full build, or rely on your CI, before you merge.

Claude Code skill

This repository is also a Claude Code plugin marketplace. Its modular plugin has a skill that helps you use modular in your own project: set up the build, declare modules from your existing packages, find what each module must require and export, fix violations, and write EARS requirements with their tests.

/plugin marketplace add fblan/modular
/plugin install modular@asymetrik

Then ask Claude, for example, "split this project into modular modules" or "why do I get an Unmanaged dependency?", or run the skill directly with /modular:modular.

Without the plugin system, copy claude/plugins/modular/skills/modular into your project's .claude/skills/ directory, or into ~/.claude/skills/ for all your projects.

Building modular

mvn clean install
Module Description
annotations @Module, @ExternalModule
processor Annotation processor for the module rules
verification ModularVerifier and ModuleAnalysis, ArchUnit-based
report ModularReport: the HTML module report
specification @Req, @TestReq and their annotation processor
sample-fizzbuzz Sample application (not published)

Design notes for each feature are in src/main/doc. Releases are made with release.sh.

License

Apache License 2.0.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages