Tech and Media Labs
This site uses cookies to improve the user experience.


Java Reflection - Modules

Jakob Jenkov
Last update: 2018-04-09

This Java module reflection tutorial will explain how to access the Java Module a Java class belongs to via Java reflection.

The concept of Java modules was added to Java 9 with the Java Platform Module System. A Java module is a set of Java packages. Thus, each Java class belongs to a package, and the package belongs to a module.

A Java module is represented by the Java reflection class java.lang.Module in the Java module java.base. Via this class you can interact with the Java Platform Module System to obtain information about a given module, or modify a module. This tutorial will cover some of the things you can do with a Module instance via Java reflection.

Obtaining a Module Instance

You can obtain an instance of the Module class via a Class instance, like this:

Module myClassModule = MyClass.class.getModule();

Is Named Module?

You can check if a Module instance a represents a named module by calling the Module isNamed() method. Here is an example:

boolean isNamed = myClassModule.isNamed();

Is Open Module?

You can check if a Module is a named module via the Module isOpen() method. Here is an example:

boolean isOpen = myClassModule.isOpen();

Obtaining a ModuleDescriptor

Once you have access to a Module instance you can access its ModuleDescriptor via the getDescriptor() method. Here is an example of accessing a Java Module's ModuleDescriptor via getDescriptor() :

ModuleDescriptor descriptor = myClassModule.getDescriptor();

From the ModuleDescriptor you can read the information in the module descriptor for the module. This Java module reflection tutorial will cover some of the information you can obtain from a module descriptor in the following sections.

Module Name

You can get the name of a named module from its module descriptor via the ModuleDescriptor name() method. Here is an example of reading the name of a Java module via reflection:

String moduleName = descriptor.name();

Exported Packages

You can read the list of packages exported by a Java module via Java reflection, via the ModuleDescriptor exports() method. Here is an example of obtaining the set of exported packages from a Java module:

Set<ModuleDescriptor.Exports> exports = descriptor.exports();

Is Automatic Module?

You can check if a Java module is an automatic module or not via the ModuleDescriptor isAutomatic() method. Here is an example of checking if a Java module is automatic or not:

boolean isAutomatic = descriptor.isAutomatic();

Is Open Module?

You can check if a Java module is an open module or not via the ModuleDescriptor isOpen() method. Here is an example of checking if a Java module is open or not:

boolean isOpen = descriptor.isOpen();

Packages in Module

You can get a list of the names of the packages in a given Java module via Java reflection. You do so via the ModuleDescriptor packages() method. Here is an example of obtaining a list of the package names for a module via reflection:

Set packages = descriptor.packages();

Services Used

You can read what services a given Java module uses via Java reflection too. The services used by a module are also referred to as the module's service dependencies. You can read the module service dependencies via the ModuleDescriptor uses() method. Here is an example of how to read the service dependencies of a Java module via reflection:

Set<String> uses = descriptor.uses();

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC