Sub topic 9
Implied readability with requires transitive
This chapter assumes you can write a basic module-info.java, you know what requires and exports do, and you can compile modules with javac --module-source-path. It covers requires transitive, which lets a module pass its own dependencies on to the modules that depend on it.
The problem: types leaking through your API
A plain requires is not transitive. Suppose module C requires B, and B requires A. Code in C can read B's exported packages. It cannot read A's packages unless C also declares requires A.
That rule causes trouble when B's public API exposes types from A. Consider a library whose public method returns a java.sql.Connection:
// src/com.example.library/module-info.java
module com.example.library {
requires java.sql;
exports com.example.library.api;
}
// src/com.example.library/com/example/library/api/Database.java
package com.example.library.api;
import java.sql.Connection;
public class Database {
public Connection open() {
throw new UnsupportedOperationException("demo only");
}
}A client module requires only com.example.library:
// src/com.example.client/module-info.java
module com.example.client {
requires com.example.library;
}
// src/com.example.client/com/example/client/Main.java
package com.example.client;
import com.example.library.api.Database;
import java.sql.Connection;
public class Main {
public static void main(String[] args) {
Database db = new Database();
Connection c = db.open();
}
}Compilation fails. The compiler reports that package java.sql is declared in module java.sql, but module com.example.client does not read it. The client can call open(), but it cannot name the return type.
You could make every client add requires java.sql. That is fragile. Each client must know about the library's internal choices, and the library's author cannot add a new API dependency without breaking every client.
The fix: requires transitive
Add the transitive modifier in the library's descriptor:
// src/com.example.library/module-info.java
module com.example.library {
requires transitive java.sql;
exports com.example.library.api;
}Now any module that reads com.example.library also reads java.sql. This is called implied readability. The client's requires com.example.library is enough, and the client code above compiles.
How the rule works
Readability is a relation between modules. The dotted edge in the diagram is not written in the client's descriptor. The module system adds it when it resolves the graph.
- If module
Mhasrequires transitive N, then every module that readsMalso readsN. - The rule chains. If
Nhasrequires transitive P, readers ofMread bothNandP. - Readability only grants access to exported packages. The client can use
java.sql.Connectionbecausejava.sqlexports that package. - A module that does not read
Mgets nothing from this.
Transitivity inside the JDK
The JDK uses this heavily. java.sql declares requires transitive java.logging and requires transitive java.xml, because its API exposes types from both. A module that requires java.sql can therefore use java.util.logging.Logger and javax.xml.transform.Source without extra directives.
You can confirm this with the tool from the earlier chapter:
java --describe-module java.sqlThe output lists requires transitive entries for these modules, and requires java.base mandated.
Aggregator modules
Some modules have no code of their own. They exist to group other modules, and they use requires transitive for all of them:
module com.example.suite {
requires transitive com.example.core;
requires transitive com.example.io;
requires transitive com.example.net;
}A client that requires com.example.suite reads all three. The JDK's java.se module is the best-known example. It requires transitively every module that makes up the Java SE API.
When to use it
Use requires transitive when a type from the required module appears in the exported API of your module. That includes:
- Method parameter and return types.
- Field types of exported public or protected members.
- Superclasses and implemented interfaces.
- Types in
throwsclauses. - Annotation types that clients must see.
Use a plain requires when the dependency is an implementation detail. Types that only appear inside method bodies, private fields, or non-exported packages do not need it.
Trade-offs and mistakes
requires transitive is part of your module's public contract. Adding it is cheap, but removing it later can break every client that relied on the implied read.
Common mistakes:
- Marking everything transitive "to be safe". Clients then read modules they never asked for, which weakens the dependency picture that jdeps and jlink rely on.
- Forgetting it when the API exposes a type. Clients get "does not read" errors that point at their code, not yours.
- Expecting transitive to export anything. It never makes a non-exported package accessible. If
java.sqldid not export a package, no amount of transitivity would let clients use it. - Using it as a replacement for
requiresinside the same module.requires transitivealready implies the module itself needs the dependency, so you do not write both.
Interaction with static
The two modifiers can be combined as requires transitive static. The dependency is optional at run time, but compile-time readers still read it. This fits an API that mentions types from an optional module.
module com.example.library {
requires transitive static java.compiler;
exports com.example.library.api;
}If java.compiler is absent at run time, the library still resolves. Code that touches its types then fails with NoClassDefFoundError, so guard such code paths.
Reading the result at run time
You can check what a module requires with --describe-module. For a compiled module in out, run:
java --module-path out --describe-module com.example.libraryThe output shows requires java.sql transitive for the library from this chapter. If a client fails to compile with a "does not read" error, check this output first. It tells you whether the library declared the dependency as transitive.
Exercise
Module app requires lib. Module lib has requires transitive json and requires logging. Code in app uses types from both json and logging, and both modules export the packages involved. Which module will fail to compile in app, and what is the smallest fix?
Show answer
Code that uses logging fails, because app reads json through implied readability but does not read logging. The smallest fix is requires logging; in app's descriptor. The better design choice, if lib exposes logging types in its exported API, is to change lib to requires transitive logging.
Exercise
A library module exports a class whose public method returns java.util.logging.Logger. The module descriptor says requires java.logging;. Clients that only require the library get a compile error when they name Logger. What one-line change in the library fixes it for all clients?
Show answer
Change the directive to requires transitive java.logging;. Every module that reads the library then also reads java.logging, so clients can name Logger without their own requires.