AnnotatedElement
public final class Module extends Object implements AnnotatedElement
named
or unnamed.
Named modules have a name
and are constructed by the
Java Virtual Machine when a graph of modules is defined to the Java virtual
machine to create a module Layer
.
An unnamed module does not have a name. There is an unnamed module for
each ClassLoader
, obtained by invoking its getUnnamedModule
method. All types that are
not in a named module are members of their defining class loader's unnamed
module.
The package names that are parameters or returned by methods defined in
this class are the fully-qualified names of the packages as defined in
section 6.5.3 of The Java™ Language Specification, for
example, "java.lang"
.
Unless otherwise specified, passing a null
argument to a method
in this class causes a NullPointerException
to
be thrown.
Class.getModule()
Modifier and Type | Method | Description |
---|---|---|
Module |
addExports(String pn,
Module other) |
If the caller's module is this module then update this module to export
the given package to the given module.
|
Module |
addOpens(String pn,
Module other) |
If this module has opened a package to at least the caller
module then update this module to open the package to the given module.
|
Module |
addReads(Module other) |
If the caller's module is this module then update this module to read
the given module.
|
Module |
addUses(Class<?> service) |
If the caller's module is this module then update this module to add a
service dependence on the given service type.
|
boolean |
canRead(Module other) |
Indicates if this module reads the given module.
|
boolean |
canUse(Class<?> service) |
Indicates if this module has a service dependence on the given service
type.
|
<T extends Annotation> |
getAnnotation(Class<T> annotationClass) |
Returns this element's annotation for the specified type if
such an annotation is present, else null.
|
Annotation[] |
getAnnotations() |
Returns annotations that are present on this element.
|
ClassLoader |
getClassLoader() |
Returns the
ClassLoader for this module. |
Annotation[] |
getDeclaredAnnotations() |
Returns annotations that are directly present on this element.
|
ModuleDescriptor |
getDescriptor() |
Returns the module descriptor for this module or
null if this
module is an unnamed module. |
Layer |
getLayer() |
Returns the layer that contains this module or
null if this
module is not in a layer. |
String |
getName() |
Returns the module name or
null if this module is an unnamed
module. |
String[] |
getPackages() |
Returns an array of the package names of the packages in this module.
|
InputStream |
getResourceAsStream(String name) |
Returns an input stream for reading a resource in this module.
|
boolean |
isExported(String pn) |
Returns
true if this module exports the given package
unconditionally. |
boolean |
isExported(String pn,
Module other) |
Returns
true if this module exports the given package to at
least the given module. |
boolean |
isNamed() |
Returns
true if this module is a named module. |
boolean |
isOpen(String pn) |
Returns
true if this module has opened a package
unconditionally. |
boolean |
isOpen(String pn,
Module other) |
Returns
true if this module has opened a package to at
least the given module. |
String |
toString() |
Returns the string representation of this module.
|
getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAnnotationPresent
public boolean isNamed()
true
if this module is a named module.true
if this is a named moduleClassLoader.getUnnamedModule()
public String getName()
null
if this module is an unnamed
module.public ClassLoader getClassLoader()
ClassLoader
for this module.
If there is a security manager then its checkPermission
method if first called with a RuntimePermission("getClassLoader")
permission to check that the caller is allowed to get access to the
class loader.
SecurityException
- If denied by the security managerpublic ModuleDescriptor getDescriptor()
null
if this
module is an unnamed module.public Layer getLayer()
null
if this
module is not in a layer.
A module Layer
contains named modules and therefore this
method always returns null
when invoked on an unnamed module.
Dynamic modules are named modules that are generated at runtime. A dynamic module may or may not be in a module Layer.
Proxy
public boolean canRead(Module other)
true
if invoked to test if this module reads itself. It also
returns true
if invoked on an unnamed module (as unnamed
modules read all modules).other
- The other moduletrue
if this module reads other
addReads(Module)
public Module addReads(Module other)
other
is this module (all modules read
themselves), this module is an unnamed module (as unnamed modules read
all modules), or this module already reads other
.other
from being GC'ed when this module is
strongly reachable.other
- The other moduleIllegalCallerException
- If this is a named module and the caller's module is not this
modulecanRead(java.lang.reflect.Module)
public boolean isExported(String pn, Module other)
true
if this module exports the given package to at
least the given module.
This method returns true
if invoked to test if a package in
this module is exported to itself. It always returns true
when
invoked on an unnamed module. A package that is open
to
the given module is considered exported to that module at run-time and
so this method returns true
if the package is open to the given
module.
This method does not check if the given module reads this module.
pn
- The package nameother
- The other moduletrue
if this module exports the package to at least the
given moduleModuleDescriptor.exports()
,
addExports(String,Module)
public boolean isOpen(String pn, Module other)
true
if this module has opened a package to at
least the given module.
This method returns true
if invoked to test if a package in
this module is open to itself. It returns true
when invoked on an
open
module with a package in the module.
It always returns true
when invoked on an unnamed module.
This method does not check if the given module reads this module.
pn
- The package nameother
- The other moduletrue
if this module has opened the package
to at least the given moduleModuleDescriptor.opens()
,
addOpens(String,Module)
,
AccessibleObject.setAccessible(boolean)
,
MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)
public boolean isExported(String pn)
true
if this module exports the given package
unconditionally.
This method always returns true
when invoked on an unnamed
module. A package that is opened
unconditionally
is considered exported unconditionally at run-time and so this method
returns true
if the package is opened unconditionally.
This method does not check if the given module reads this module.
pn
- The package nametrue
if this module exports the package unconditionallyModuleDescriptor.exports()
public boolean isOpen(String pn)
true
if this module has opened a package
unconditionally.
This method always returns true
when invoked on an unnamed
module. Additionally, it always returns true
when invoked on an
open
module with a package in the
module.
This method does not check if the given module reads this module.
pn
- The package nametrue
if this module has opened the package
unconditionallyModuleDescriptor.opens()
public Module addExports(String pn, Module other)
This method has no effect if the package is already exported (or open) to the given module.
pn
- The package nameother
- The moduleIllegalArgumentException
- If pn
is null
, or this is a named module and the
package pn
is not a package in this moduleIllegalCallerException
- If this is a named module and the caller's module is not this
moduleisExported(String,Module)
public Module addOpens(String pn, Module other)
This method has no effect if the package is already open to the given module.
pn
- The package nameother
- The moduleIllegalArgumentException
- If pn
is null
, or this is a named module and the
package pn
is 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 moduleisOpen(String,Module)
,
AccessibleObject.setAccessible(boolean)
,
MethodHandles.privateLookupIn(java.lang.Class<?>, java.lang.invoke.MethodHandles.Lookup)
public Module addUses(Class<?> service)
ServiceLoader
on behalf of other modules or where the framework is
passed a reference to the service type by other code. This method is
a no-op when invoked on an unnamed module or an automatic module.
This method does not cause resolveAndBind
to be re-run.
service
- The service typeIllegalCallerException
- If this is a named module and the caller's module is not this
modulecanUse(Class)
,
ModuleDescriptor.uses()
public boolean canUse(Class<?> service)
true
when invoked on an unnamed
module or an automatic module.service
- The service typetrue
if this module uses service type st
addUses(Class)
public String[] getPackages()
For named modules, the returned array contains an element for each package in the module. It may contain elements corresponding to packages added to the module, dynamic modules for example, after it was loaded.
For unnamed modules, this method is the equivalent to invoking the
getDefinedPackages
method of
this module's class loader and returning the array of package names.
A package name appears at most once in the returned array.
Set
for
consistency with other java.lang.reflect
types.public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
null
when invoked on an unnamed module.getAnnotation
in interface AnnotatedElement
T
- the type of the annotation to query for and return if presentannotationClass
- the Class object corresponding to the
annotation typepublic Annotation[] getAnnotations()
getAnnotations
in interface AnnotatedElement
public Annotation[] getDeclaredAnnotations()
getDeclaredAnnotations
in interface AnnotatedElement
public InputStream getResourceAsStream(String name) throws IOException
name
parameter is a '/'
-separated path name that
identifies the resource. As with Class.getResourceAsStream
, this method delegates to the module's class
loader findResource(String,String)
method, invoking it with the module name
(or null
when the module is unnamed) and the name of the
resource. If the resource name has a leading slash then it is dropped
before delegation.
A resource in a named module may be encapsulated so that it cannot be located by code in other modules. Whether a resource can be located or not is determined as follows:
.class
" then it is not
encapsulated. package
in the module
then the resource can only be located by the caller of this method
when the package is open
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, the package name for a resource is derived
from the subsequence of characters that precedes the last '/'
in
the name and then replacing each '/'
character in the subsequence
with '.'
. A leading slash is ignored when deriving the package
name. As an example, the package name derived for a resource named
"a/b/c/foo.properties
" is "a.b.c
". A resource name
with the name "META-INF/MANIFEST.MF
" is never encapsulated
because "META-INF
" is not a legal package name.
This method returns null
if the resource is not in this
module, the resource is encapsulated and cannot be located by the caller,
or access to the resource is denied by the security manager.
name
- The resource namenull
IOException
- If an I/O error occursClass.getResourceAsStream(String)
public String toString()
"module"
, followed by a space,
and then the module name. For an unnamed module, the representation is
the string "unnamed module"
, followed by a space, and then an
implementation specific string that identifies the unnamed module. Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-internal+0-adhoc.mlchung.jdk9-jdeps