Java Module System for Mortals MahmoudAnouti / Marc Kassis
Motivation • Makes it easier for developers to construct and maintain libraries and large applications • Improve the security and maintainability of Java SE Platform Implementations in general, and the JDK in particular
Motivation • Enable improved application performance • Enable the Java SE Platform, and the JDK, to scale down for use in small computing devices and dense cloud deployments.
Goals of the Module System Reliable configuration: solve classpath issues Strong encapsulation: ability to define public APIs for a component Scalable Java SE: ability to assemble custom runtime image Encapsulate internal APIs (sun.* classes) Improved performance
What is a module? A named, self-describing set of program components consisting of code (Java classes, interfaces) and data (resources / config files)
What is a module? classes < packages < modules
Module declaration module-info.java: 1. Module name 2. Required modules (dependencies) 3. Exported packages module com.example.myapp { requires com.example.lib; exports com.example.foo; exports com.example.foo.bar; } (1) (2) (3)
Module declaration • Dependencies: A module definition can require other modules to compile and run. • Exports: A module can export one or more of its packages for use by other modules. Code that accesses API in non-exported packages will fail (both compile and runtime).
Module declaration • Resources in a module are accessible only by code within that module. • Services: a module can declare that it uses a service interface whose implementations may be provided by other modules. • Resolution:The module system resolves all transitive dependencies required by an initial module  result is a configuration
module-info.java keywords module A{ requires transitive B; exports com.example to D; opens com.example.beans; opens com.example.private to E; }
Services module com.example.serviceapi { exports com.example.myservice; } module com.example.serviceimpl { requires com.example.serviceapi; provides com.example.myservice.MyServiceInterface with com.example.impl.MyServiceImpl; }
Services module com.example.client { requires com.example.serviceapi; uses com.example.myservice.MyServiceInterface; }
DEMO
Module artifacts • A module can be packaged into a modularJAR file: META-INF/ META-INF/MANIFEST.MF module-info.class com/example/foo/Main.class com/example/foo/bar/Bar.class ...
Platform modules java.activation java.base java.compiler java.corba java.datatransfer java.desktop java.instrument java.logging java.management java.management.rmi java.naming java.prefs java.rmi java.scripting java.se java.se.ee java.security.jgss java.security.sasl java.sql java.sql.rowset java.transaction java.xml java.xml.bind java.xml.crypto java.xml.ws java.xml.ws.annotation Java SE platform is divided into modules
Platform modules module java.base { exports java.io; exports java.lang; exports java.lang.annotation; exports java.lang.invoke; exports java.lang.module; exports java.lang.ref; exports java.lang.reflect; exports java.math; exports java.net; exports java.util; ... }
Module path • The module system resolves modules by locating them in a module path: sequence of elements, where each element is either:  a module artifact (e.g. modular JAR), or  a directory containing module artifacts Classpath is still there!
DEMO
Reliable configuration module com.example.myapp { requires com.example.lib; requires org.apache.commons.io; } module com.example.lib { requires java.sql; exports com.example.utils; }
Reliable configuration com.example.myapp com.example.lib java.sql java.xml java.base org.apache.commons.io java.logging
Reliable configuration •Every dependency is fulfilled by precisely one other module •The module graph is acyclic •Every module reads at most one module defining a given package •Modules defining identically-named packages do not interfere with each other.
requires transitive module java.sql { requires transitive java.logging; requires transitive java.xml; exports java.sql; exports javax.sql; exports javax.transaction.xa; }
Implied readability com.example.myapp com.example.lib java.sql java.xml java.base org.apache.commons.io java.logging
Strong encapsulation If two types App and Util are defined in different modules, and Util is public, then code in App can access Util iff: 1. App’s module requires (i.e. reads) Util’s module, and 2. Util’s module exports Util’s package. public is no longer public!
Migrating to modular code • An existing classpath app running on Java 8 will continue to run on Java 9, so long as it only uses standard, non-deprecated Java SE APIs. • Code that does not belong to a defined module, belongs to the unnamed module.
The unnamed module • Contains types that exist on the classpath • Reads (i.e. depends) all other modules, including platform modules • Exports all packages, ... • A explicit named module cannot read the unnamed module
Bottom-up migration com.example.app com.example.orm com.example.utils java.sql java.base java.logging
Bottom-up migration com.example.app com.example.orm java.sql java.base java.logging com.example.utils
Bottom-up migration com.example.app java.sql java.base java.logging com.example.utils com.example.orm
Bottom-up migration java.sql java.base java.logging com.example.utils com.example.orm com.example.app
DEMO
Top-down migration com.example.app com.example.orm com.example.utils java.sql java.base java.logging
Top-down migration java.sql java.base java.logging com.example.utils com.example.orm com.example.app Automatic modules
Automatic modules • Its name is derived from Automatic-Module-Name in MANIFEST.MF, or from the JAR file name • Non-modular JAR that gets added on the module path • Exports all its packages • Reads all other modules • Reads the unnamed module
DEMO

Java modulesystem

Editor's Notes

  • #15 Mention JMOD artifact format