Class ModuleDescriptor
- All Implemented Interfaces:
Comparable<ModuleDescriptor>
A module descriptor describes a named module and defines methods toobtain each of its components. The module descriptor for a named modulein the Java virtual machine is obtained by invoking theModule'sgetDescriptor method. Module descriptors can also be created using theModuleDescriptor.Builder class or by reading the binary form of amodule declaration (module-info.class) using theread methods defined here.
A module descriptor describes anormal, open, or automaticmodule.Normal modules and open modules describe theirdependences,exported-packages, the servicesthat theyuse orprovide, and othercomponents.Normal modules mayopen specificpackages. The module descriptor for an open module does not declare anyopen packages (itsopens method returns an empty set) but wheninstantiated in the Java virtual machine then it is treated as if allpackages are open. The module descriptor for an automatic module does notdeclare any dependences (except for the mandatory dependency onjava.base), and does not declare any exported or open packages. Automaticmodules receive special treatment during resolution so that they read allother modules in the configuration. When an automatic module is instantiatedin the Java virtual machine then it reads every unnamed module and istreated as if all packages are exported and open.
ModuleDescriptor objects are immutable and safe for use bymultiple concurrent threads.
- Since:
- 9
- See Also:
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA builder for buildingModuleDescriptorobjects.static final classA package exported by a module, may be qualified or unqualified.static enumA modifier on a module.static final classA package opened by a module, may be qualified or unqualified.static final classA service that a module provides one or more implementations of.static final classA dependence upon a module.static final classA module's version string.Method Summary
Modifier and TypeMethodDescriptionReturns the set of themodule flags.intcompareTo(ModuleDescriptor that) Compares this module descriptor to another.booleanTests this module descriptor for equality with the given object.exports()Returns the set ofExportsobjects representing the exportedpackages.inthashCode()Computes a hash code for this module descriptor.booleanReturnstrueif this is an automatic module.booleanisOpen()Returnstrueif this is an open module.Returns the module main class.Returns the set of module modifiers.name()Returns the module name.staticModuleDescriptor.BuildernewAutomaticModule(String name) Instantiates a builder to build a module descriptor for an automaticmodule.staticModuleDescriptor.BuilderInstantiates a builder to build a module descriptor for anormalmodule.staticModuleDescriptor.BuildernewModule(String name,Set<ModuleDescriptor.Modifier> ms) Instantiates a builder to build a module descriptor.staticModuleDescriptor.BuildernewOpenModule(String name) Instantiates a builder to build a module descriptor for an open module.opens()Returns the set ofOpensobjects representing the openpackages.packages()Returns the set of packages in the module.provides()Returns the set ofProvidesobjects representing theservices that the module provides.Returns the string with the possibly-unparseable version of themodule.staticModuleDescriptorread(InputStream in) Reads the binary form of a module declaration from an input stream as amodule descriptor.staticModuleDescriptorread(InputStream in,Supplier<Set<String>> packageFinder) Reads the binary form of a module declaration from an input streamas a module descriptor.staticModuleDescriptorread(ByteBuffer bb) Reads the binary form of a module declaration from a byte buffer as amodule descriptor.staticModuleDescriptorread(ByteBuffer bb,Supplier<Set<String>> packageFinder) Reads the binary form of a module declaration from a byte bufferas a module descriptor.requires()Returns the set ofRequiresobjects representing the moduledependences.Returns a string containing the module name and, if present, itsversion.toString()Returns a string describing the module.uses()Returns the set of service dependences.version()Returns the module version.
Method Details
name
modifiers
Returns the set of module modifiers.
- Returns:
- A possibly-empty unmodifiable set of modifiers
accessFlags
Returns the set of themodule flags.- Returns:
- A possibly-empty unmodifiable set of module flags
- SeeJava Virtual Machine Specification:
- 4.7.25 The Module Attribute
- Since:
- 20
- See Also:
isOpen
isAutomatic
requires
Returns the set of
Requiresobjects representing the moduledependences.The set includes a dependency on "
java.base" when thismodule is not named "java.base". If this module is an automaticmodule then it does not have a dependency on any module other than"java.base".- Returns:
- A possibly-empty unmodifiable set of
ModuleDescriptor.Requiresobjects
exports
Returns the set of
Exportsobjects representing the exportedpackages.If this module is an automatic module then the set of exportsis empty.
- Returns:
- A possibly-empty unmodifiable set of exported packages
opens
Returns the set of
Opensobjects representing the openpackages.If this module is an open module or an automatic module then theset of open packages is empty.
- Returns:
- A possibly-empty unmodifiable set of open packages
uses
Returns the set of service dependences.
If this module is an automatic module then the set of servicedependences is empty.
- Returns:
- A possibly-empty unmodifiable set of thebinary names of the service types used
provides
Returns the set of
Providesobjects representing theservices that the module provides.- Returns:
- The possibly-empty unmodifiable set of the services that this module provides
version
Returns the module version.
- Returns:
- This module's version, or an empty
Optionalif the module does not have a version or the version isunparseable
rawVersion
toNameAndVersion
Returns a string containing the module name and, if present, itsversion.
- Returns:
- A string containing the module name and, if present, its version
mainClass
Returns the module main class.
- Returns:
- Thebinary name of the module's main class
packages
compareTo
Compares this module descriptor to another.Two
ModuleDescriptorobjects are compared by comparing theirmodule names lexicographically. Where the module names are equal then themodule versions are compared. When comparing the module versions then amodule descriptor with a version is considered to succeed a moduledescriptor that does not have a version. If both versions areunparseable then theraw version strings are compared lexicographically. Where the module namesare equal and the versions are equal (or not present in both), then theset of modifiers are compared. Sets of modifiers are compared by comparingabinary value computed for each set. If a modifier is presentin the set then the bit at the position of its ordinal is1in the binary value, otherwise0. If the two set of modifiersare also equal then the other components of the module descriptors arecompared in a manner that is consistent withequals.- Specified by:
compareToin interfaceComparable<ModuleDescriptor>- Parameters:
that- The module descriptor to compare- Returns:
- A negative integer, zero, or a positive integer if this module descriptor is less than, equal to, or greater than the given module descriptor
equals
Tests this module descriptor for equality with the given object.If the given object is not a
ModuleDescriptorthen thismethod returnsfalse. Two module descriptors are equal if eachof their corresponding components is equal.This method satisfies the general contract of the
Object.equalsmethod.hashCode
public int hashCode()Computes a hash code for this module descriptor.The hash code is based upon the components of the module descriptor,and satisfies the general contract of the
Object.hashCodemethod.toString
newModule
Instantiates a builder to build a module descriptor.- Parameters:
name- The module namems- The set of module modifiers- Returns:
- A new builder
- Throws:
IllegalArgumentException- If the module name isnullor is not a legal module name, or the set of modifiers containsAUTOMATICwith other modifiers
newModule
Instantiates a builder to build a module descriptor for anormalmodule. This method is equivalent to invokingnewModulewith an empty set ofmodifiers.- Parameters:
name- The module name- Returns:
- A new builder
- Throws:
IllegalArgumentException- If the module name isnullor is not a legal module name
newOpenModule
Instantiates a builder to build a module descriptor for an open module.This method is equivalent to invokingnewModulewith theOPENmodifier.The builder for an open module cannot be used to declare any openpackages.
- Parameters:
name- The module name- Returns:
- A new builder that builds an open module
- Throws:
IllegalArgumentException- If the module name isnullor is not a legal module name
newAutomaticModule
Instantiates a builder to build a module descriptor for an automaticmodule. This method is equivalent to invokingnewModulewith theAUTOMATICmodifier.The builder for an automatic module cannot be used to declare moduleor service dependences. It also cannot be used to declare any exportedor open packages.
- Parameters:
name- The module name- Returns:
- A new builder that builds an automatic module
- Throws:
IllegalArgumentException- If the module name isnullor is not a legal module name- See Also:
read
public static ModuleDescriptor read(InputStream in,Supplier<Set<String>> packageFinder) throwsIOException Reads the binary form of a module declaration from an input streamas a module descriptor.If the descriptor encoded in the input stream does not indicate aset of packages in the module then the
packageFinderwill beinvoked. The set of packages that thepackageFinderreturnsmust include all the packages that the module exports, opens, as wellas the packages of the service implementations that the module provides,and the package of the main class (if the module has a main class). IfthepackageFinderthrows anUncheckedIOExceptionthenIOExceptioncause will be re-thrown.If there are bytes following the module descriptor then it isimplementation specific as to whether those bytes are read, ignored,or reported as an
InvalidModuleDescriptorException. If thismethod fails with anInvalidModuleDescriptorExceptionorIOExceptionthen it may do so after some, but not all, bytes havebeen read from the input stream. It is strongly recommended that thestream be promptly closed and discarded if an exception occurs.- API Note:
- The
packageFinderparameter is for use when readingmodule descriptors from legacy module-artifact formats that do notrecord the set of packages in the descriptor itself. - Parameters:
in- The input streampackageFinder- A supplier that can produce the set of packages- Returns:
- The module descriptor
- Throws:
InvalidModuleDescriptorException- If an invalid module descriptor is detected or the set of packages returned by thepackageFinderdoes not include all of the packages obtained from the module descriptorIOException- If an I/O error occurs reading from the input stream orUncheckedIOExceptionis thrown by the package finder
read
Reads the binary form of a module declaration from an input stream as amodule descriptor. This method works exactly as specified by the 2-argreadmethod with the exception thata package finder is not used to find additional packages when themodule descriptor read from the stream does not indicate the set ofpackages.- Parameters:
in- The input stream- Returns:
- The module descriptor
- Throws:
InvalidModuleDescriptorException- If an invalid module descriptor is detectedIOException- If an I/O error occurs reading from the input stream
read
Reads the binary form of a module declaration from a byte bufferas a module descriptor.If the descriptor encoded in the byte buffer does not indicate aset of packages in the module then the
packageFinderwill beinvoked. The set of packages that thepackageFinderreturnsmust include all the packages that the module exports, opens, as wellas the packages of the service implementations that the module provides,and the package of the main class (if the module has a main class). IfthepackageFinderthrows anUncheckedIOExceptionthenIOExceptioncause will be re-thrown.The module descriptor is read from the buffer starting at index
p, wherepis the buffer'spositionwhen this method is invoked. Upon return the buffer's positionwill be equal top + nwherenis the number of bytesread from the buffer.If there are bytes following the module descriptor then it isimplementation specific as to whether those bytes are read, ignored,or reported as an
InvalidModuleDescriptorException. If thismethod fails with anInvalidModuleDescriptorExceptionthen itmay do so after some, but not all, bytes have been read.- API Note:
- The
packageFinderparameter is for use when readingmodule descriptors from legacy module-artifact formats that do notrecord the set of packages in the descriptor itself. - Parameters:
bb- The byte bufferpackageFinder- A supplier that can produce the set of packages- Returns:
- The module descriptor
- Throws:
InvalidModuleDescriptorException- If an invalid module descriptor is detected or the set of packages returned by thepackageFinderdoes not include all of the packages obtained from the module descriptor
read
Reads the binary form of a module declaration from a byte buffer as amodule descriptor. This method works exactly as specified by the 2-argreadmethod with the exception that apackage finder is not used to find additional packages when the moduledescriptor encoded in the buffer does not indicate the set of packages.- Parameters:
bb- The byte buffer- Returns:
- The module descriptor
- Throws:
InvalidModuleDescriptorException- If an invalid module descriptor is detected