mainClass
public Optional<String> mainClass()
Returns the module main class.
- Returns:
- The fully qualified class name of the module's main class
Comparable<ModuleDescriptor>
public class ModuleDescriptor extends Object implements Comparable<ModuleDescriptor>
A module descriptor describes a named module and defines methods to obtain each of its components. The module descriptor for a named module in the Java virtual machine is obtained by invoking the Module
's getDescriptor
method. Module descriptors can also be created using the ModuleDescriptor.Builder
class or by reading the binary form of a module declaration (module-info.class
) using the read
methods defined here.
A module descriptor describes a normal, open, or automatic module. Normal modules and open modules describe their dependences, exported-packages
, the services that they use or provide, and other components. Normal modules may open specific packages. The module descriptor for an open module does not declare any open packages (its opens
method returns an empty set) but when instantiated in the Java virtual machine then it is treated as if all packages are open. The module descriptor for an automatic module does not declare any dependences (except for the mandatory dependency on
java.base
), and does not declare any exported or open packages. Automatic modules receive special treatment during resolution so that they read all other modules in the configuration. When an automatic module is instantiated in the Java virtual machine then it reads every unnamed module and is treated as if all packages are exported and open.
ModuleDescriptor
objects are immutable and safe for use by multiple concurrent threads.
Modifier and Type | Class | Description |
---|---|---|
static final class |
ModuleDescriptor.Builder |
A builder for building ModuleDescriptor objects. |
static final class |
ModuleDescriptor.Exports |
A package exported by a module, may be qualified or unqualified. |
static enum |
ModuleDescriptor.Modifier |
A modifier on a module. |
static final class |
ModuleDescriptor.Opens |
A package opened by a module, may be qualified or unqualified. |
static final class |
ModuleDescriptor.Provides |
A service that a module provides one or more implementations of. |
static final class |
ModuleDescriptor.Requires |
A dependence upon a module. |
static final class |
ModuleDescriptor.Version |
A module's version string. |
Modifier and Type | Method | Description |
---|---|---|
Set |
accessFlags() |
Returns the set of the module flags. |
int |
compareTo |
Compares this module descriptor to another. |
boolean |
equals |
Tests this module descriptor for equality with the given object. |
Set |
exports() |
Returns the set of Exports objects representing the exported packages. |
int |
hashCode() |
Computes a hash code for this module descriptor. |
boolean |
isAutomatic() |
Returns true if this is an automatic module. |
boolean |
isOpen() |
Returns true if this is an open module. |
Optional |
mainClass() |
Returns the module main class. |
Set |
modifiers() |
Returns the set of module modifiers. |
String |
name() |
Returns the module name. |
static ModuleDescriptor.Builder |
newAutomaticModule |
Instantiates a builder to build a module descriptor for an automatic module. |
static ModuleDescriptor.Builder |
newModule |
Instantiates a builder to build a module descriptor for a normal module. |
static ModuleDescriptor.Builder |
newModule |
Instantiates a builder to build a module descriptor. |
static ModuleDescriptor.Builder |
newOpenModule |
Instantiates a builder to build a module descriptor for an open module. |
Set |
opens() |
Returns the set of Opens objects representing the open packages. |
Set |
packages() |
Returns the set of packages in the module. |
Set |
provides() |
Returns the set of Provides objects representing the services that the module provides. |
Optional |
rawVersion() |
Returns the string with the possibly-unparseable version of the module. |
static ModuleDescriptor |
read |
Reads the binary form of a module declaration from an input stream as a module descriptor. |
static ModuleDescriptor |
read |
Reads the binary form of a module declaration from an input stream as a module descriptor. |
static ModuleDescriptor |
read |
Reads the binary form of a module declaration from a byte buffer as a module descriptor. |
static ModuleDescriptor |
read |
Reads the binary form of a module declaration from a byte buffer as a module descriptor. |
Set |
requires() |
Returns the set of Requires objects representing the module dependences. |
String |
toNameAndVersion() |
Returns a string containing the module name and, if present, its version. |
String |
toString() |
Returns a string describing the module. |
Set |
uses() |
Returns the set of service dependences. |
Optional |
version() |
Returns the module version. |
public String name()
Returns the module name.
public Set<ModuleDescriptor.Modifier> modifiers()
Returns the set of module modifiers.
public Set<AccessFlag> accessFlags()
public boolean isOpen()
Returns true
if this is an open module.
This method is equivalent to testing if the set of modifiers
contains the OPEN
modifier.
true
if this is an open modulepublic boolean isAutomatic()
Returns true
if this is an automatic module.
This method is equivalent to testing if the set of modifiers
contains the AUTOMATIC
modifier.
true
if this is an automatic modulepublic Set<ModuleDescriptor.Requires> requires()
Returns the set of Requires
objects representing the module dependences.
The set includes a dependency on "java.base
" when this module is not named "java.base
". If this module is an automatic module then it does not have a dependency on any module other than "java.base
".
ModuleDescriptor.Requires
objectspublic Set<ModuleDescriptor.Exports> exports()
Returns the set of Exports
objects representing the exported packages.
If this module is an automatic module then the set of exports is empty.
public Set<ModuleDescriptor.Opens> opens()
Returns the set of Opens
objects representing the open packages.
If this module is an open module or an automatic module then the set of open packages is empty.
public Set<String> uses()
Returns the set of service dependences.
If this module is an automatic module then the set of service dependences is empty.
public Set<ModuleDescriptor.Provides> provides()
Returns the set of Provides
objects representing the services that the module provides.
public Optional<ModuleDescriptor.Version> version()
Returns the module version.
Optional
if the module does not have a version or the version is unparseable
public Optional<String> rawVersion()
Returns the string with the possibly-unparseable version of the module.
Optional
if the module does not have a versionpublic String toNameAndVersion()
Returns a string containing the module name and, if present, its version.
public Optional<String> mainClass()
Returns the module main class.
public Set<String> packages()
The set of packages includes all exported and open packages, as well as the packages of any service providers, and the package for the main class.
public int compareTo(ModuleDescriptor that)
Two ModuleDescriptor
objects are compared by comparing their module names lexicographically. Where the module names are equal then the module versions are compared. When comparing the module versions then a module descriptor with a version is considered to succeed a module descriptor that does not have a version. If both versions are unparseable then the raw version strings are compared lexicographically. Where the module names are equal and the versions are equal (or not present in both), then the set of modifiers are compared. Sets of modifiers are compared by comparing a binary value computed for each set. If a modifier is present in the set then the bit at the position of its ordinal is 1
in the binary value, otherwise 0
. If the two set of modifiers are also equal then the other components of the module descriptors are compared in a manner that is consistent with equals
.
compareTo
in interface Comparable<ModuleDescriptor>
that
- The module descriptor to comparepublic boolean equals(Object ob)
If the given object is not a ModuleDescriptor
then this method returns false
. Two module descriptors are equal if each of their corresponding components is equal.
This method satisfies the general contract of the Object.equals
method.
public int hashCode()
The hash code is based upon the components of the module descriptor, and satisfies the general contract of the Object.hashCode
method.
public String toString()
Returns a string describing the module.
public static ModuleDescriptor.Builder newModule(String name, Set<ModuleDescriptor.Modifier> ms)
name
- The module namems
- The set of module modifiersIllegalArgumentException
- If the module name is null
or is not a legal module name, or the set of modifiers contains AUTOMATIC
with other modifierspublic static ModuleDescriptor.Builder newModule(String name)
newModule
with an empty set of modifiers
.name
- The module nameIllegalArgumentException
- If the module name is null
or is not a legal module namepublic static ModuleDescriptor.Builder newOpenModule(String name)
newModule
with the OPEN
modifier. The builder for an open module cannot be used to declare any open packages.
name
- The module nameIllegalArgumentException
- If the module name is null
or is not a legal module namepublic static ModuleDescriptor.Builder newAutomaticModule(String name)
newModule
with the AUTOMATIC
modifier. The builder for an automatic module cannot be used to declare module or service dependences. It also cannot be used to declare any exported or open packages.
name
- The module nameIllegalArgumentException
- If the module name is null
or is not a legal module namepublic static ModuleDescriptor read(InputStream in, Supplier<Set<String>> packageFinder) throws IOException
If the descriptor encoded in the input stream does not indicate a set of packages in the module then the packageFinder
will be invoked. The set of packages that the packageFinder
returns must include all the packages that the module exports, opens, as well as the packages of the service implementations that the module provides, and the package of the main class (if the module has a main class). If the packageFinder
throws an UncheckedIOException
then IOException
cause will be re-thrown.
If there are bytes following the module descriptor then it is implementation specific as to whether those bytes are read, ignored, or reported as an InvalidModuleDescriptorException
. If this method fails with an InvalidModuleDescriptorException
or
IOException
then it may do so after some, but not all, bytes have been read from the input stream. It is strongly recommended that the stream be promptly closed and discarded if an exception occurs.
packageFinder
parameter is for use when reading module descriptors from legacy module-artifact formats that do not record the set of packages in the descriptor itself.in
- The input streampackageFinder
- A supplier that can produce the set of packagesInvalidModuleDescriptorException
- If an invalid module descriptor is detected or the set of packages returned by the packageFinder
does not include all of the packages obtained from the module descriptorIOException
- If an I/O error occurs reading from the input stream or
UncheckedIOException
is thrown by the package finderpublic static ModuleDescriptor read(InputStream in) throws IOException
read
method with the exception that a package finder is not used to find additional packages when the module descriptor read from the stream does not indicate the set of packages.in
- The input streamInvalidModuleDescriptorException
- If an invalid module descriptor is detectedIOException
- If an I/O error occurs reading from the input streampublic static ModuleDescriptor read(ByteBuffer bb, Supplier<Set<String>> packageFinder)
If the descriptor encoded in the byte buffer does not indicate a set of packages in the module then the packageFinder
will be invoked. The set of packages that the packageFinder
returns must include all the packages that the module exports, opens, as well as the packages of the service implementations that the module provides, and the package of the main class (if the module has a main class). If the packageFinder
throws an UncheckedIOException
then IOException
cause will be re-thrown.
The module descriptor is read from the buffer starting at index p
, where p
is the buffer's position
when this method is invoked. Upon return the buffer's position will be equal to p + n
where n
is the number of bytes read from the buffer.
If there are bytes following the module descriptor then it is implementation specific as to whether those bytes are read, ignored, or reported as an InvalidModuleDescriptorException
. If this method fails with an InvalidModuleDescriptorException
then it may do so after some, but not all, bytes have been read.
packageFinder
parameter is for use when reading module descriptors from legacy module-artifact formats that do not record the set of packages in the descriptor itself.bb
- The byte bufferpackageFinder
- A supplier that can produce the set of packagesInvalidModuleDescriptorException
- If an invalid module descriptor is detected or the set of packages returned by the packageFinder
does not include all of the packages obtained from the module descriptorpublic static ModuleDescriptor read(ByteBuffer bb)
read
method with the exception that a package finder is not used to find additional packages when the module descriptor encoded in the buffer does not indicate the set of packages.bb
- The byte bufferInvalidModuleDescriptorException
- If an invalid module descriptor is detected
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/module/ModuleDescriptor.html