Java module-info Essentials, from Binary code point of view
Java module system has been introduced since Java 9, via the project Jigsaw. Each java module contains a module-info.class file, for the specification of current module. Here we are trying to make an introduction of the module-info file.
Binary Code is always the best document.
So let's inspect an example module-info: module info in java.base.jmod file, which is the biggest java module in current OpenJDK.
We can open the module-info.class file using Java Class Viewer, as the screen shots bellow.
The module-info.class file is a special type of .class file, and it has the following key differences from other normal .class file:
1. module-info.class do not have super class, interfaces, fields, methods.
2. module-info.class contains the Module attribute
The Module attribute specify the super details of current attribute, the full details could be found from the Java VM Specification.
Here we will highlight several interesting attributes of the Module attribute:
Here is the details of the exports section, which contains the Java packages exported by current module, like:
Here is the details of the uses section, which contains the Java service interface used by current module, like:
Here is the details of the provides section, which contains the Java service implementation provided by current module. Current module provides one service implementation:
3. module-info.class contains the ModulePackages attribute
The ModulePackages attribute indicates all the packages of a module that are exported or opened by the Module attribute, as well as all the packages of the service implementations recorded in the Module attribute. The full details could be found from the Java VM Specification.
The following screen shot is an example values for ModulePackages attribute in module-info.class from java.base.jmod from OpenJDK 11:
Binary Code is always the best document.
So let's inspect an example module-info: module info in java.base.jmod file, which is the biggest java module in current OpenJDK.
- Source code: master/src/java.base/share/classes/module-info.java
- The binary file is located at: JAVA_HOME/jmods/java.base.jmod - classes/module-info.class
We can open the module-info.class file using Java Class Viewer, as the screen shots bellow.
The module-info.class file is a special type of .class file, and it has the following key differences from other normal .class file:
1. module-info.class do not have super class, interfaces, fields, methods.
- From the screen shot bellow, we can find that the super_class node's value is "0", which means this module-info.class do not have a super class.
- All other class will have a super/parent class; if the parent class did not specified in the source code, the java.lang.Object would be the super_class by default; this relationship would be added by the Java compiler (javac) automatically.
- interfaces_count is "0" (zero), which means current module-info.class do not implement any interfaces
- fields_count is "0" (zero), which means current module-info.class do not contain any fields
- methods_count is "0" (zero), which means current module-info.class do not contain any methods
module-info.class overview for java.base.jmod in OpenJDK 11, via Java Class Viewer |
2. module-info.class contains the Module attribute
The Module attribute specify the super details of current attribute, the full details could be found from the Java VM Specification.
Here we will highlight several interesting attributes of the Module attribute:
- module_name_index: which is an index pointing to the module name: "java.base" in the following example
- module_version_index: which is an index pointing to the module version: "11.0.4" in the following example, which is the OpenJDK version containing current module
- exports_count and exports: the Java packages exported by current module. Current module exports 107 Java packages
- uses_count and uses: the Java service interface used by current module. Current module uses 33 Java service interfaces, which the current module may discover via java.util.ServiceLoader
- provides_count and provides: the Java service implementation provided by current module. Current module provides 1 Java service interface implementation
Module attribute in module-info.class for java.base.jmod in OpenJDK 11, via Java Class Viewer |
- java.io
- java.lang
- java.lang.annotation
- java.lang.invoke
- java.lang.module
- java.lang.ref
- java.lang.reflect
- java.math
- java.net
- etc.
Exports section in Module attribute in module-info.class for java.base.jmod in OpenJDK 11, via Java Class Viewer |
Here is the details of the uses section, which contains the Java service interface used by current module, like:
- java.lang.System$LoggerFinder
- java.net.ContentHandlerFactory
- java.net.spi.URLStreamHandlerProvider
- java.nio.channels.spi.AsynchronousChannelProvider
- java.nio.channels.spi.SelectorProvider
- java.nio.charset.spi.CharsetProvider
- java.nio.file.spi.FileSystemProvider
- java.nio.file.spi.FileTypeDetector
- java.security.Provider
- etc.
Uses section in Module attribute in module-info.class for java.base.jmod in OpenJDK 11, via Java Class Viewer |
Here is the details of the provides section, which contains the Java service implementation provided by current module. Current module provides one service implementation:
- jdk.internal.jrtfs.JrtFileSystemProvider for java.nio.file.spi.FileSystemProvider
Exports section in Module attribute in module-info.class for java.base.jmod in OpenJDK 11, via Java Class Viewer |
3. module-info.class contains the ModulePackages attribute
The ModulePackages attribute indicates all the packages of a module that are exported or opened by the Module attribute, as well as all the packages of the service implementations recorded in the Module attribute. The full details could be found from the Java VM Specification.
The following screen shot is an example values for ModulePackages attribute in module-info.class from java.base.jmod from OpenJDK 11:
- sun.security.ssl
- sun.reflect.annotation
- sun.net.ftp
- sun.net.www.protocol.http
- jdk.internal.jimage
- sun.security.tools
- com.sun.net.ssl
- java.nio
- sun.net.www.content.text
- etc.
ModulePackages attribute in module-info.class for java.base.jmod in OpenJDK 11, via Java Class Viewer |
Comments
Post a Comment