Class Module
- All Implemented Interfaces:
AnnotatedElement
named or unnamed. Named modules have aname and are constructed by theJava Virtual Machine when a graph of modules is defined to the Java virtualmachine to create amodule layer.
An unnamed module does not have a name. There is an unnamed module foreachClassLoader, obtained by invoking itsgetUnnamedModule method. All types that arenot in a named module are members of their defining class loader's unnamedmodule.
The package names that are parameters or returned by methods defined inthis class are the fully-qualified names of the packages as defined insection6.5.3 ofThe Java Language Specification, forexample,"java.lang".
Unless otherwise specified, passing anull argument to a methodin this class causes aNullPointerException tobe thrown.
- SeeJava Language Specification:
- 7.7 Module Declarations
- Since:
- 9
- See Also:
Method Summary
Modifier and TypeMethodDescriptionaddExports(String pn,Module other) If the caller's module is this module then update this module to exportthe given package to the given module.If this module hasopened a package to at least the callermodule then update this module to open the package to the given module.If the caller's module is this module then update this module to readthe given module.If the caller's module is this module then update this module to add aservice dependence on the given service type.booleanIndicates if this module reads the given module.booleanIndicates if this module has a service dependence on the given servicetype.<T extendsAnnotation>
TgetAnnotation(Class<T> annotationClass) Returns this element's annotation for the specified type ifsuch an annotation ispresent, else null.Returns annotations that arepresent on this element.Returns theClassLoaderfor this module.Returns annotations that aredirectly present on this element.Returns the module descriptor for this module ornullif thismodule is an unnamed module.getLayer()Returns the module layer that contains this module ornullifthis module is not in a module layer.getName()Returns the module name ornullif this module is an unnamedmodule.Returns the set of package names for the packages in this module.getResourceAsStream(String name) Returns an input stream for reading a resource in this module.booleanisExported(String pn) Returnstrueif this module exports the given packageunconditionally.booleanisExported(String pn,Module other) Returnstrueif this module exports the given package to atleast the given module.booleanisNamed()Returnstrueif this module is a named module.booleanReturnstrueif this module can accessrestricted methods.booleanReturnstrueif this module hasopened a packageunconditionally.booleanReturnstrueif this module hasopened a package to atleast the given module.toString()Returns the string representation of this module.Methods declared in class Object
clone,equals,finalize,getClass,hashCode,notify,notifyAll,wait,wait,waitMethods declared in interface AnnotatedElement
getAnnotationsByType,getDeclaredAnnotation,getDeclaredAnnotationsByType,isAnnotationPresent
Method Details
isNamed
public boolean isNamed()Returnstrueif this module is a named module.- Returns:
trueif this is a named module- SeeJava Language Specification:
- 7.7.5 Unnamed Modules
- See Also:
getName
Returns the module name ornullif this module is an unnamedmodule.- Returns:
- The module name
getClassLoader
Returns theClassLoaderfor this module.- Returns:
- The class loader for this module
getDescriptor
Returns the module descriptor for this module ornullif thismodule is an unnamed module.- Returns:
- The module descriptor for this module
getLayer
Returns the module layer that contains this module ornullifthis module is not in a module layer.A module layer contains named modules and therefore this method alwaysreturnsnullwhen invoked on an unnamed module.Dynamic modules arenamed modules that are generated at runtime. A dynamic module may or maynot be in a module layer.
- Returns:
- The module layer that contains this module
- See Also:
isNativeAccessEnabled
public boolean isNativeAccessEnabled()Returnstrueif this module can accessrestricted methods.- Returns:
trueif this module can accessrestricted methods.- Since:
- 22
canRead
Indicates if this module reads the given module. This method returnstrueif invoked to test if this module reads itself. It alsoreturnstrueif invoked on an unnamed module (as unnamedmodules read all modules).- Parameters:
other- The other module- Returns:
trueif this module readsother- See Also:
addReads
If the caller's module is this module then update this module to readthe given module.This method is a no-op ifotheris this module (all modules readthemselves), this module is an unnamed module (as unnamed modules readall modules), or this module already readsother.- Implementation Note:
- Read edges added by this method areweak anddo not prevent
otherfrom being GC'ed when this module isstrongly reachable. - Parameters:
other- The other module- Returns:
- this module
- Throws:
IllegalCallerException- If this is a named module and the caller's module is not this module- See Also:
isExported
Returnstrueif this module exports the given package to atleast the given module.This method returns
trueif invoked to test if a package inthis module is exported to itself. It always returnstruewheninvoked on an unnamed module. A package that isopentothe given module is considered exported to that module at run-time andso this method returnstrueif the package is open to the givenmodule.This method does not check if the given module reads this module.
- Parameters:
pn- The package nameother- The other module- Returns:
trueif this module exports the package to at least the given module- See Also:
isOpen
Returnstrueif this module hasopened a package to atleast the given module.This method returns
trueif invoked to test if a package inthis module is open to itself. It returnstruewhen invoked on anopenmodule with a package in the module.It always returnstruewhen invoked on an unnamed module.This method does not check if the given module reads this module.
- API Note:
- A package
popened to moduleMallows code inMdodeep reflection on all types in the package.Further, ifMreads this module, it can obtain aLookupobject that is allowed todefine classesin packagep. - Parameters:
pn- The package nameother- The other module- Returns:
trueif this module hasopened the package to at least the given module- See Also:
isExported
Returnstrueif this module exports the given packageunconditionally.This method always returns
truewhen invoked on an unnamedmodule. A package that isopenedunconditionallyis considered exported unconditionally at run-time and so this methodreturnstrueif the package is opened unconditionally.This method does not check if the given module reads this module.
- Parameters:
pn- The package name- Returns:
trueif this module exports the package unconditionally- See Also:
isOpen
Returnstrueif this module hasopened a packageunconditionally.This method always returns
truewhen invoked on an unnamedmodule. Additionally, it always returnstruewhen invoked on anopenmodule with a package in themodule.This method does not check if the given module reads this module.
- API Note:
- A package
popened to moduleMallows code inMdodeep reflection on all types in the package.Further, ifMreads this module, it can obtain aLookupobject that is allowed todefine classesin packagep. - Parameters:
pn- The package name- Returns:
trueif this module hasopened the package unconditionally- See Also:
addExports
If the caller's module is this module then update this module to exportthe given package to the given module.This method has no effect if the package is already exported (oropen) to the given module.
- API Note:
- As specified in section5.4.3 of theThe JavaVirtual Machine Specification, if an attempt to resolve asymbolic reference fails because of a linkage error, then subsequentattempts to resolve the reference always fail with the same error thatwas thrown as a result of the initial resolution attempt.
- Parameters:
pn- The package nameother- The module- Returns:
- this module
- Throws:
IllegalArgumentException- Ifpnisnull, or this is a named module and the packagepnis not a package in this moduleIllegalCallerException- If this is a named module and the caller's module is not this module- SeeJava Virtual Machine Specification:
- 5.4.3 Resolution
- See Also:
addOpens
If this module hasopened a package to at least the callermodule then update this module to open the package to the given module.Opening a package with this method allows all types in the package,and all their members, not just public types and their public members,to be reflected on by the given module when using APIs that supportprivate access or a way to bypass or suppress default Java languageaccess control checks.This method has no effect if the package is alreadyopento the given module.
- API Note:
- This method can be used for cases where aconsumermodule uses a qualified opens to open a package to anAPImodule but where the reflective access to the members of classes inthe consumer module is delegated to code in another module. Code in theAPI module can use this method to open the package in the consumer moduleto the other module.
- Parameters:
pn- The package nameother- The module- Returns:
- this module
- Throws:
IllegalArgumentException- Ifpnisnull, or this is a named module and the packagepnis not a package in this moduleIllegalCallerException- If this is a named module and this module has not opened the package to at least the caller's module- See Also:
addUses
If the caller's module is this module then update this module to add aservice dependence on the given service type. This method is intendedfor use by frameworks that invokeServiceLoaderon behalf of other modules or where the framework ispassed a reference to the service type by other code. This method isa no-op when invoked on an unnamed module or an automatic module.This method does not cause
resolveAndBindto be re-run.- Parameters:
service- The service type- Returns:
- this module
- Throws:
IllegalCallerException- If this is a named module and the caller's module is not this module- See Also:
canUse
Indicates if this module has a service dependence on the given servicetype. This method always returnstruewhen invoked on an unnamedmodule or an automatic module.- Parameters:
service- The service type- Returns:
trueif this module uses service typest- See Also:
getPackages
Returns the set of package names for the packages in this module.For named modules, the returned set contains an element for eachpackage in the module.
For unnamed modules, the returned set contains an element foreach package that
has been definedin the unnamed module.- Returns:
- the set of the package names of the packages in this module
getAnnotation
Returns this element's annotation for the specified type ifsuch an annotation ispresent, else null.This method returnsnullwhen invoked on an unnamed module.Note that any annotation returned by this method is adeclaration annotation.
- Specified by:
getAnnotationin interfaceAnnotatedElement- Type Parameters:
T- the type of the annotation to query for and return if present- Parameters:
annotationClass- the Class object corresponding to the annotation type- Returns:
- this element's annotation for the specified annotation type if present on this element, else null
getAnnotations
Returns annotations that arepresent on this element.If there are no annotationspresent on this element, the returnvalue is an array of length 0.The caller of this method is free to modify the returned array; it willhave no effect on the arrays returned to other callers.This method returns an empty array when invoked on an unnamed module.Note that any annotations returned by this method aredeclaration annotations.
- Specified by:
getAnnotationsin interfaceAnnotatedElement- Returns:
- annotations present on this element
getDeclaredAnnotations
Returns annotations that aredirectly present on this element.This method ignores inherited annotations.If there are no annotationsdirectly present on this element,the return value is an array of length 0.The caller of this method is free to modify the returned array; it willhave no effect on the arrays returned to other callers.This method returns an empty array when invoked on an unnamed module.Note that any annotations returned by this method aredeclaration annotations.
- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Returns:
- annotations directly present on this element
getResourceAsStream
Returns an input stream for reading a resource in this module.Thenameparameter is a'/'-separated path name thatidentifies the resource. As withClass.getResourceAsStream, this method delegates to the module's classloaderfindResource(String,String)method, invoking it with the module name(ornullwhen the module is unnamed) and the name of theresource. If the resource name has a leading slash then it is droppedbefore delegation.A resource in a named module may beencapsulated so thatit cannot be located by code in other modules. Whether a resource can belocated or not is determined as follows:
- If the resource name ends with "
.class" then it is not encapsulated. - Apackage name is derived from the resource name. If the package name is apackage in the module then the resource can only be located by the caller of this method when the package isopen to at least the caller's module. If the resource is not in a package in the module then the resource is not encapsulated.
In the above, thepackage name for a resource is derivedfrom the subsequence of characters that precedes the last
'/'inthe name and then replacing each'/'character in the subsequencewith'.'. A leading slash is ignored when deriving the packagename. As an example, the package name derived for a resource named"a/b/c/foo.properties" is "a.b.c". A resource namewith the name "META-INF/MANIFEST.MF" is never encapsulatedbecause "META-INF" is not a legal package name.This method returns
nullif the resource is not in this moduleor the resource is encapsulated and cannot be located by the caller.- Parameters:
name- The resource name- Returns:
- An input stream for reading the resource or
null - Throws:
IOException- If an I/O error occurs- See Also:
- If the resource name ends with "
toString
Returns the string representation of this module. For a named module,the representation is the string"module", followed by a space,and then the module name. For an unnamed module, the representation isthe string"unnamed module", followed by a space, and then animplementation specific string that identifies the unnamed module.