public final class Module extends Object
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
per ClassLoader
that is obtained by invoking the class
loader's getUnnamedModule
method. The
getModule
method of all types defined by a class
loader that are not in a named module return the 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 and Description |
---|---|
Module |
addExports(String pn,
Module other)
If the caller's module is this module then update this module to export
package
pn 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<?> st)
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<?> st)
Indicates if this module has a service dependence on the given service
type.
|
ClassLoader |
getClassLoader()
Returns the
ClassLoader for this module. |
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. |
String |
toString()
Returns the string representation of this module.
|
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 can
read themselves) or this module is an unnamed module (as unnamed modules
read all modules).other
- The other moduleIllegalStateException
- If this is a named module and the caller 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 always return 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 exports the package to at least the
given modulepublic boolean isExported(String pn)
true
if this module exports the given package
unconditionally.
This method always return true
when invoked on an unnamed
module.
This method does not check if the given module reads this module
pn
- The package nametrue
if this module exports the package unconditionallypublic Module addExports(String pn, Module other)
pn
to the given module.
This method has no effect if the package is already exported to the given module. It also has no effect if invoked on an unnamed module (as unnamed modules export all packages).
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 moduleIllegalStateException
- If this is a named module and the caller is not this modulepublic Module addUses(Class<?> st)
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.
This method does not cause resolveRequiresAndUses
to be
re-run.
st
- The service typeIllegalStateException
- If this is a named module and the caller is not this modulecanUse(Class)
,
ModuleDescriptor.uses()
public boolean canUse(Class<?> st)
true
when invoked on an unnamed
module.st
- 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 of 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 InputStream getResourceAsStream(String name) throws IOException
null
if the resource is not in this module or access to the
resource is denied by the security manager.
The name
is a '/'
-separated path name that identifies
the resource.
If this module is an unnamed module, and the ClassLoader
for
this module is not null
, then this method is equivalent to
invoking the getResourceAsStream
method on the class loader for this module.
name
- The resource namenull
IOException
- If an I/O error occursModuleReader.open(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, 2016, Oracle and/or its affiliates. All rights reserved.
DRAFT 9-internal+0-2016-08-10-225032.mimcmah.jdk9-sandbox