A layer is created from a graph of modules in a Configuration
and a function that maps each module to a ClassLoader
.
Creating a layer informs the Java virtual machine about the classes that
may be loaded from the modules so that the Java virtual machine knows which
module that each class is a member of.
Creating a layer creates a Module
object for each ResolvedModule
in the configuration. For each resolved module that is
read
, the Module
reads
the corresponding run-time Module
, which may
be in the same layer or a parent
layer.
The defineModulesWithOneLoader
and
defineModulesWithManyLoaders
methods
provide convenient ways to create a module layer where all modules are
mapped to a single class loader or where each module is mapped to its own
class loader. The defineModules
method is for more
advanced cases where modules are mapped to custom class loaders by means of
a function specified to the method. Each of these methods has an instance
and static variant. The instance methods create a layer with the receiver
as the parent layer. The static methods are for more advanced cases where
there can be more than one parent layer or where a Controller
is needed to control modules in the layer
A Java virtual machine has at least one non-empty layer, the boot
layer, that is created when the Java virtual machine is
started. The boot layer contains module java.base
and is the only
layer in the Java virtual machine with a module named "java.base
".
The modules in the boot layer are mapped to the bootstrap class loader and
other class loaders that are
built-in into the Java virtual machine. The boot layer will often be
the parent
when creating additional layers.
Each Module
in a layer is created so that it exports
and opens
the packages described by its ModuleDescriptor
. Qualified exports
(where a package is exported to a set of target modules rather than all
modules) are reified when creating the layer as follows:
X
exports a package to Y
, and if the
runtime Module
X
reads Module
Y
, then
the package is exported to Module
Y
(which may be in
the same layer as X
or a parent layer). X
exports a package to Y
, and if the
runtime Module
X
does not read Y
then target
Y
is located as if by invoking findModule
to find the module in the layer or its parent layers. If
Y
is found then the package is exported to the instance of
Y
that was found. If Y
is not found then the qualified
export is ignored. Qualified opens are handled in same way as qualified exports.
As when creating a Configuration
,
automatic
modules receive special
treatment when creating a layer. An automatic module is created in the
Java virtual machine as a Module
that reads every unnamed Module
in the Java virtual machine.
Unless otherwise specified, passing a null
argument to a method
in this class causes a NullPointerException
to
be thrown.
This example creates a configuration by resolving a module named
"myapp
" with the configuration for the boot layer as the parent. It
then creates a new layer with the modules in this configuration. All modules
are defined to the same class loader.
ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration()
.resolve(finder, ModuleFinder.of(), Set.of("myapp"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
Class<?> c = layer.findLoader("myapp").loadClass("app.Main");
Module#getLayer()
Modifier and Type | Class and Description |
---|---|
public static class | ModuleLayer.
Controls a module layer. |
Modifier and Type | Field and Description |
---|---|
private volatile List | |
private final Configuration | |
private static final ClassLoaderValue | |
private static ModuleLayer | |
private volatile Set | |
private final Map | |
private final List | |
private volatile ServicesCatalog |
Access | Constructor and Description |
---|---|
private | ModuleLayer(Configuration cf, List<ModuleLayer> parents, Function<String, ClassLoader> clf)
Creates a new module layer from the modules in the given configuration. |
Modifier and Type | Method and Description |
---|---|
pack-priv boolean | Returns: true iff the module is present in this layer,
false otherwisethe name of the module for which the native access
should be enabled name)Updates the module with the given |
pack-priv void | bindToLoader(ClassLoader loader)
Record that this layer has at least one module defined to the given class loader. |
public static ModuleLayer | |
private static void | checkConfiguration(Configuration cf, List<ModuleLayer> parentLayers)
Checks that the parent configurations match the configuration of the parent layers. |
private static void | checkForDuplicatePkgs(Configuration cf, Function<String, ClassLoader> clf)
Checks a configuration and the module-to-loader mapping to ensure that no two modules mapped to the same class loader have the same package. |
public Configuration | |
public ModuleLayer | Returns: The newly created layerThe configuration for the layer cf, Function<String, ClassLoader> The function to map a module name to a class loader clf)Creates a new module layer, with this layer as its parent, by defining the
modules in the given |
public static ModuleLayer. | Returns: A controller that controls the newly created layerThe configuration for the layer cf, List<ModuleLayer> The list of parent layers in search order parentLayers, Function<String, ClassLoader> The function to map a module name to a class loader clf)Creates a new module layer by defining the modules in the given |
public ModuleLayer | Returns: The newly created layerThe configuration for the layer cf, ClassLoader The parent class loader for each of the class loaders created by
this method; may be parentLoader)null for the bootstrap class loaderCreates a new module layer, with this layer as its parent, by defining the
modules in the given |
public static ModuleLayer. | Returns: A controller that controls the newly created layerThe configuration for the layer cf, List<ModuleLayer> The list of parent layers in search order parentLayers, ClassLoader The parent class loader for each of the class loaders created by
this method; may be parentLoader)null for the bootstrap class loaderCreates a new module layer by defining the modules in the given |
public ModuleLayer | Returns: The newly created layerThe configuration for the layer cf, ClassLoader The parent class loader for the class loader created by this
method; may be parentLoader)null for the bootstrap class loaderCreates a new module layer, with this layer as its parent, by defining the
modules in the given |
public static ModuleLayer. | Returns: A controller that controls the newly created layerThe configuration for the layer cf, List<ModuleLayer> The list of parent layers in search order parentLayers, ClassLoader The parent class loader for the class loader created by this
method; may be parentLoader)null for the bootstrap class loaderCreates a new module layer by defining the modules in the given |
public static ModuleLayer | |
private static LayerInstantiationException | |
public ClassLoader | Returns: The ClassLoader that the module is defined toThe name of the module to find name)Returns the |
public Optional | Returns: The module with the given name or an emptyOptional
if there isn't a module with this name in this layer or any
parent layerThe name of the module to find name)Returns the module with the given name in this layer, or if not in this layer, the parent layers. |
pack-priv ServicesCatalog | |
pack-priv Stream | |
pack-priv static Stream | layers(ClassLoader loader)
Returns a stream of the layers that have at least one module defined to the given class loader. |
public Set | Returns: A possibly-empty unmodifiable set of the modules in this layerReturns an unmodifiable set of the modules in this layer. |
public List | Returns: A possibly-empty unmodifiable list of this layer's parentsReturns an unmodifiable list of this layer's parents, in search order. |
public String | Returns: A possibly empty string describing this module layerOverrides java. Returns a string describing this module layer. |
allLayers | back to summary |
---|---|
private volatile List<ModuleLayer> allLayers |
cf | back to summary |
---|---|
private final Configuration cf |
CLV | back to summary |
---|---|
private static final ClassLoaderValue<List<ModuleLayer>> CLV |
EMPTY_LAYER | back to summary |
---|---|
private static ModuleLayer EMPTY_LAYER
|
modules | back to summary |
---|---|
private volatile Set<Module> modules |
nameToModule | back to summary |
---|---|
private final Map<String, Module> nameToModule |
parents | back to summary |
---|---|
private final List<ModuleLayer> parents |
servicesCatalog | back to summary |
---|---|
private volatile ServicesCatalog servicesCatalog |
ModuleLayer | back to summary |
---|---|
private ModuleLayer(Configuration cf, List<ModuleLayer> parents, Function<String, ClassLoader> clf) Creates a new module layer from the modules in the given configuration. |
addEnableNativeAccess | back to summary |
---|---|
pack-priv boolean addEnableNativeAccess(String name) Updates the module with the given
|
bindToLoader | back to summary |
---|---|
pack-priv void bindToLoader(ClassLoader loader) Record that this layer has at least one module defined to the given class loader. |
boot | back to summary |
---|---|
public static ModuleLayer boot() Returns the boot layer. The boot layer contains at least one module,
API Note This method returns
|
checkConfiguration | back to summary |
---|---|
private static void checkConfiguration(Configuration cf, List<ModuleLayer> parentLayers) Checks that the parent configurations match the configuration of the parent layers. |
checkForDuplicatePkgs | back to summary |
---|---|
private static void checkForDuplicatePkgs(Configuration cf, Function<String, ClassLoader> clf) Checks a configuration and the module-to-loader mapping to ensure that no two modules mapped to the same class loader have the same package. It also checks that no two automatic modules have the same package. |
configuration | back to summary |
---|---|
public Configuration configuration() Returns the configuration for this layer.
|
defineModules | back to summary |
---|---|
public ModuleLayer defineModules(Configuration cf, Function<String, ClassLoader> clf) Creates a new module layer, with this layer as its parent, by defining the
modules in the given
|
defineModules | back to summary |
---|---|
public static ModuleLayer. Creates a new module layer by defining the modules in the given The class loader delegation implemented by the class loaders must
respect module readability. The class loaders should be
Creating a layer can fail for the following reasons:
In addition, a layer cannot be created if the configuration contains
a module named " If the function to map a module name to class loader throws an error or runtime exception then it is propagated to the caller of this method. API Note It is implementation specific as to whether creating a layer with this method is an atomic operation or not. Consequently it is possible for this method to fail with some modules, but not all, defined to the Java virtual machine.
|
defineModulesWithManyLoaders | back to summary |
---|---|
public ModuleLayer defineModulesWithManyLoaders(Configuration cf, ClassLoader parentLoader) Creates a new module layer, with this layer as its parent, by defining the
modules in the given
|
defineModulesWithManyLoaders | back to summary |
---|---|
public static ModuleLayer. Creates a new module layer by defining the modules in the given The class loaders created by this method implement direct
delegation when loading classes from modules. If the The class loaders created by this method locate resources
(
|
defineModulesWithOneLoader | back to summary |
---|---|
public ModuleLayer defineModulesWithOneLoader(Configuration cf, ClassLoader parentLoader) Creates a new module layer, with this layer as its parent, by defining the
modules in the given
|
defineModulesWithOneLoader | back to summary |
---|---|
public static ModuleLayer. Creates a new module layer by defining the modules in the given The class loader created by this method implements direct
delegation when loading classes from modules. If the The class loader created by this method locates resources
( Attempting to create a layer with all modules defined to the same class loader can fail for the following reasons:
In addition, a layer cannot be created if the configuration contains
a module named "
|
empty | back to summary |
---|---|
public static ModuleLayer empty() Returns the empty layer. There are no modules in the empty layer. It has no parents.
|
fail | back to summary |
---|---|
private static LayerInstantiationException fail(String fmt, Object... args) Creates a LayerInstantiationException with the message formatted from the given format string and arguments. |
findLoader | back to summary |
---|---|
public ClassLoader findLoader(String name) Returns the API Note This method does not return an
|
findModule | back to summary |
---|---|
public Optional Returns the module with the given name in this layer, or if not in this
layer, the parent layers. Finding a module in
parent layers is equivalent to invoking |
getServicesCatalog | back to summary |
---|---|
pack-priv ServicesCatalog getServicesCatalog() Returns the ServicesCatalog for this Layer, creating it if not already created. |
layers | back to summary |
---|---|
pack-priv Stream Returns an ordered stream of layers. The first element is this layer, the remaining elements are the parent layers in DFS order. Implementation Note For now, the assumption is that the number of elements will be very low and so this method does not use a specialized spliterator. |
layers | back to summary |
---|---|
pack-priv static Stream Returns a stream of the layers that have at least one module defined to the given class loader. |
modules | back to summary |
---|---|
public Set Returns an unmodifiable set of the modules in this layer. |
parents | back to summary |
---|---|
public List Returns an unmodifiable list of this layer's parents, in search order. If this is the empty layer then an empty list is returned.
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a string describing this module layer. |
ModuleLayer
to create module layers return a Controller
that can be used to
control modules in the layer.
Unless otherwise specified, passing a null
argument to a
method in this class causes a NullPointerException
to be thrown.
API Note
Care should be taken with Controller
objects, they
should never be shared with untrusted code.
Modifier and Type | Field and Description |
---|---|
private final ModuleLayer |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public ModuleLayer. | Returns: This controllerThe source module source, String The package name pn, Module The target module target)Updates module |
public ModuleLayer. | |
public ModuleLayer. | |
public ModuleLayer. | Returns: This controllerThe module to update target)Enables native access for a module in the layer if the caller's module has native access. |
private void | |
public ModuleLayer |
layer | back to summary |
---|---|
private final ModuleLayer layer |
Controller | back to summary |
---|---|
pack-priv Controller(ModuleLayer layer) |
addExports | back to summary |
---|---|
public ModuleLayer. Updates module
|
addOpens | back to summary |
---|---|
public ModuleLayer. Updates module
|
addReads | back to summary |
---|---|
public ModuleLayer. Updates module Implementation Note Read edges added by this method are weak
and do not prevent
|
enableNativeAccess | back to summary |
---|---|
public ModuleLayer. Enables native access for a module in the layer if the caller's module has native access.
|
ensureInLayer | back to summary |
---|---|
private void ensureInLayer(Module source) |
layer | back to summary |
---|---|
public ModuleLayer layer() Returns the layer that this object controls.
|