Quiz: Java Modules: module-info, requires, exports and opens

Total: 6 questions

1. 

What is a module in Java (JPMS) and how is the module-info.java file structured?

A module is a named set of packages described by a module-info.java file. In it the module itself lists which modules it needs (requires), which packages are available to others (exports) and which are open to reflection (opens). Everything else is hidden, even public classes. The module system, JPMS (Java Platform Module System), arrived in Java 9. It fixes three class-path problems: no encapsulation between JARs, no dependency checks at startup, and one package possibly sitting in two JARs.

  • Location: the file with the fixed name module-info.java sits in the root of the module sources, next to the package directories, and compiles to module-info.class.
  • Module name follows the package-name rules: identifiers separated by dots, usually reverse-domain (com.shop.catalog). Hyphens, keywords and parts starting with a digit are not allowed: my-app, com.2app, com.int.app do not compile, while my_app and transitive (a contextual keyword) do.
  • Directives (requires, exports, opens, uses/provides) go inside the braces, and their order does not matter.

Modules are optional: without module-info.java code runs on the class path, inside the unnamed module. It reads all modules and exports all its packages, and getModule().getName() returns null for it. A JPMS module is not the same as a Maven module, which is described by pom.xml.

2. 

What does the requires directive do, and when does the module system check dependencies?

requires M; creates a "module reads module" relation (readability). Code in a module can use types of another module only if it reads that module; otherwise javac reports package ... is not visible (... but module ... does not read it).

  • java.base is read implicitly by every module, so java.util.List, java.time.LocalDate, java.nio.file.Path are available without requires. In --describe-module output this is the line requires java.base mandated. An explicit requires java.base; is allowed but changes nothing. Other JDK modules, such as java.sql, must be required explicitly.
  • At compile time: requires of a missing module gives error: module not found.
  • At startup: the JVM checks the whole module graph before starting. If a module was deleted after compilation, the program does not start even if the code would never reach it: Error occurred during initialization of boot layer, java.lang.module.FindException: Module ... not found, required by ....
  • Cycles are forbidden: if m.a requires m.b and m.b requires m.a, the compiler reports cyclic dependence. Even requires m.x; inside m.x itself is a cycle.

Requiring the same module twice is the error duplicate requires.

3. 

How does the exports directive work, and when is a public class accessible from another module?

A public class is accessible from another module only if both conditions hold: its package is exported (exports) and the other module reads this module (requires). javac tells you what is missing: which does not export it means no exports, does not read it means no requires.

  • You export a package, not a class: exports com.shop.catalog.api.Catalog; and wildcards like exports com.shop.*; do not compile.
  • Subpackages are not exported with the package: com.shop.catalog and com.shop.catalog.api are different packages. Exporting an empty or missing package is the error package is empty or does not exist; exporting it twice is duplicate or conflicting exports.
  • Only public and protected members are visible; package-private classes and private fields stay closed.
  • A qualified export exports P to M1, M2; opens the package only to the listed modules; the others get which does not export it to module .... A missing module in the to list is not an error, only the warning module not found.

Split package: if two modules export the same package, a module that reads both does not compile (reads package ... from both ...), even when they contain different classes.

4. 

What is the difference between requires transitive and requires static?

Plain readability is not transitive: if com.shop.catalog requires com.shop.model, the module com.shop.app with requires com.shop.catalog cannot name the type Product from com.shop.model (does not read it).

requires transitive M: every module that reads this module automatically reads M too. Use it when a type from M appears in the public API of an exported package (return type, parameter, superclass). The javac -Xlint:exports flag warns about a missing one: ... is not indirectly exported using 'requires transitive'. The JDK works the same way: java.sql requires java.logging and java.xml transitively. If the dependency is used only inside the implementation, a plain requires is enough.

requires static M: M is mandatory at compile time and optional at run time. Such a module is not added to the graph, even if it is on the module path, until it is added with --add-modules or someone requires it with a plain requires. Check for it with ModuleLayer.boot().findModule("...").isPresent(); calling a class of an absent module without the check fails with NoClassDefFoundError. This is how optional features are wired in: metrics, annotations for tools. The combination requires transitive static is also allowed.

5. 

How does opens differ from exports, and what is an open module?

exports gives normal access to the package's public types at compile time and at run time. opens allows deep reflection at run time, including setAccessible(true) on private fields. Frameworks like Hibernate, Jackson and Spring need it.

  • Only exports: code in another module compiles, reflection on public works, and setAccessible(true) on private throws InaccessibleObjectException (module ... does not "opens ..." to module ...).
  • Only opens: reflection works, but plain code using the package does not compile (is not visible), because opens works only at run time.
  • exports + opens: everything is available; this declaration compiles.

Variants: opens P to M; opens the package to module M only. open module M { } opens all packages of the module to reflection; the opens directive is not allowed inside it ('opens' only allowed in strong modules), and exports is still needed for normal access. The launcher option --add-opens module/package=target opens a package without editing module-info, for example --add-opens java.base/java.lang=ALL-UNNAMED for class-path code: JDK modules are closed to deep reflection.

6. 

How do you inspect a module with java --describe-module, and how do you read its output?

The java --describe-module command (short form -d) shows what a module requires, exports and opens: java -p out --describe-module com.shop.catalog for your own module or java --describe-module java.sql for a JDK module.

  • The modifier comes after the name: requires com.shop.model transitive, not requires transitive com.shop.model.
  • The line requires java.base mandated appears for every module, even if its module-info says nothing about java.base.
  • Non-exported packages are printed as contains (for example contains com.shop.catalog.internal); exports to specific modules appear as qualified exports ... to ....
  • For an open module (open module) the first line ends with the word open after the path.

For example, the output for java.sql contains requires java.logging transitive and requires java.xml transitive: this module returns their types in its API.

Page 1 of 1