A configuration encapsulates the readability graph that is the
output of resolution. A readability graph is a directed graph whose vertices
are of type ResolvedModule and the edges represent the readability
amongst the modules. Configuration defines the modules() method to get the set of resolved modules in the graph. ResolvedModule defines the reads() method to
get the set of modules that a resolved module reads. The modules that are
read may be in the same configuration or may be in parent
configurations.
Configuration defines the resolve method to resolve a collection of root modules, and the resolveAndBind
method to do resolution with service binding. There are instance and
static variants of both methods. The instance methods create a configuration
with the receiver as the parent configuration. The static methods are for
more advanced cases where there can be more than one parent configuration.
Each layer of modules in the Java virtual
machine is created from a configuration. The configuration for the boot layer is obtained by invoking ModuleLayer.boot().configuration(). The configuration for the boot layer
will often be the parent when creating new configurations.
M 'uses' a service or
'provides' an implementation of a service, then the service must be available
to M at run time, either because M itself contains the service's
package or because M reads another module that exports the service's package.
However, it is sometimes desirable for the service's package to come from a module
that is optional at run time, as indicated by the use of 'requires static' in this
example:
module M {
requires static Y;
uses p.S;
}
module Y {
exports p;
}
M has an optional dependency on some module
Y, but Y is not needed at run time (Y might be observable but
no-one reads it), then resolution at run time assumes that Y exported
the service's package at compile time. Resolution at run time does not attempt to
check whether Y is observable or (if it is observable) whether Y
exports the service's package.
The module that 'uses' the service, or 'provides' an implementation
of it, may depend directly on the optional module, as M does above, or may
depend indirectly on the optional module, as shown here:
module M {
requires X;
uses p.S;
}
module X {
requires static transitive Y;
}
module Y {
exports p;
}
M 'uses', or 'provides' an
implementation of, is optional if it comes from an optional dependency. In this case,
code in M must be prepared to deal with the class or interface that denotes
the service being unavailable at run time. This is distinct from the more regular
case where the service is available but no implementations of the service are
available.
The following example uses the resolve method to resolve a
module named myapp with the configuration for the boot layer as the
parent configuration. It prints the name of each resolved module and the
names of the modules that each module reads.
Path dir1 = ..., dir2 = ..., dir3 = ...;
ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
Configuration parent = ModuleLayer.boot().configuration();
Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of("myapp"));
cf.modules().forEach(m -> {
System.out.format("%s -> %s%n",
m.name(),
m.reads().stream()
.map(ResolvedModule::name)
.collect(Collectors.joining(", ")));
});
java.lang.ModuleLayer
| Modifier and Type | Field and Description |
|---|---|
| private volatile List | |
| private static Configuration | |
| private final Map | |
| private final Set | |
| private final Map | |
| private final List | |
| private final String |
| Access | Constructor and Description |
|---|---|
| private | |
| private | |
| pack-priv | Configuration(ModuleFinder finder, Map<String, Set<String>> map)
Creates the Configuration for the boot layer from a pre-generated readability graph. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv Stream | |
| pack-priv Set | |
| public static Configuration | |
| public Optional | Returns: The resolved module with the given name or an emptyOptional if there isn't a module with this name in this
configuration or any parent configurationsThe module name of the resolved module to find name)Finds a resolved module in this configuration, or if not in this configuration, the parent configurations. |
| public Set | Returns: A possibly-empty unmodifiable set of the resolved modules in this configurationReturns an unmodifiable set of the resolved modules in this configuration. |
| public List | Returns: A possibly-empty unmodifiable list of this parent configurationsReturns an unmodifiable list of this configuration's parents, in search order. |
| pack-priv Set | |
| public Configuration | Returns: The configuration that is the result of resolving the given root modulesThe before module finder to find modules before, ModuleFinder The after module finder to locate modules when not
located by the after, Collection<String> before module finder or in parent
configurationsThe possibly-empty collection of module names of the modules
to resolve roots)Resolves a collection of root modules, with this configuration as its parent, to create a new configuration. |
| public static Configuration | Returns: The configuration that is the result of resolving the given root modulesThe before module finder to find modules before, List<Configuration> The list parent configurations in search order parents, ModuleFinder The after module finder to locate modules when not
located by the after, Collection<String> before module finder or in parent
configurationsThe possibly-empty collection of module names of the modules
to resolve roots)Resolves a collection of root modules to create a configuration. |
| public Configuration | Returns: The configuration that is the result of resolving, with service binding, the given root modulesThe before module finder to find modules before, ModuleFinder The after module finder to locate modules when not
located by the after, Collection<String> before module finder or in parent
configurationsThe possibly-empty collection of module names of the modules
to resolve roots)Resolves a collection of root modules, with service binding, and with this configuration as its parent, to create a new configuration. |
| pack-priv static Configuration | resolveAndBind(ModuleFinder finder, Collection<String> roots, PrintStream traceOutput)
Resolves a collection of root modules, with service binding, and with the empty configuration as its parent. |
| public static Configuration | Returns: The configuration that is the result of resolving, with service binding, the given root modulesThe before module finder to find modules before, List<Configuration> The list parent configurations in search order parents, ModuleFinder The after module finder to locate modules when not
located by the after, Collection<String> before module finder or in parent
configurationsThe possibly-empty collection of module names of the modules
to resolve roots)Resolves a collection of root modules, with service binding, to create configuration. |
| pack-priv String | |
| public String | Returns: A possibly empty string describing this configurationOverrides java. Returns a string describing this configuration. |
| allConfigurations | back to summary |
|---|---|
| private volatile List<Configuration> allConfigurations | |
| EMPTY_CONFIGURATION | back to summary |
|---|---|
| private static Configuration EMPTY_CONFIGURATION
| |
| graph | back to summary |
|---|---|
| private final Map<ResolvedModule, Set<ResolvedModule>> graph | |
| modules | back to summary |
|---|---|
| private final Set<ResolvedModule> modules | |
| nameToModule | back to summary |
|---|---|
| private final Map<String, ResolvedModule> nameToModule | |
| parents | back to summary |
|---|---|
| private final List<Configuration> parents | |
| targetPlatform | back to summary |
|---|---|
| private final String targetPlatform | |
| Configuration | back to summary |
|---|---|
| private Configuration() | |
| Configuration | back to summary |
|---|---|
| private Configuration(List<Configuration> parents, Resolver resolver) | |
| Configuration | back to summary |
|---|---|
| pack-priv Configuration(ModuleFinder finder, Map<String, Set<String>> map) Creates the Configuration for the boot layer from a pre-generated readability graph. API Note This method is coded for startup performance. | |
| configurations | back to summary |
|---|---|
| pack-priv Stream Returns an ordered stream of configurations. The first element is this configuration, the remaining elements are the parent configurations 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. | |
| descriptors | back to summary |
|---|---|
| pack-priv Set | |
| empty | back to summary |
|---|---|
| public static Configuration empty() Returns the empty configuration. There are no modules in the empty configuration. It has no parents.
| |
| findModule | back to summary |
|---|---|
| public Optional Finds a resolved module in this configuration, or if not in this
configuration, the parent configurations.
Finding a module in parent configurations is equivalent to invoking
| |
| modules | back to summary |
|---|---|
| public Set Returns an unmodifiable set of the resolved modules in this configuration.
| |
| parents | back to summary |
|---|---|
| public List Returns an unmodifiable list of this configuration's parents, in search order. If this is the empty configuration then an empty list is returned.
| |
| reads | back to summary |
|---|---|
| pack-priv Set | |
| resolve | back to summary |
|---|---|
| public Configuration resolve(ModuleFinder before, ModuleFinder after, Collection<String> roots) Resolves a collection of root modules, with this configuration as its
parent, to create a new configuration. This method works exactly as
specified by the static
| |
| resolve | back to summary |
|---|---|
| public static Configuration resolve(ModuleFinder before, List<Configuration> parents, ModuleFinder after, Collection<String> roots) Resolves a collection of root modules to create a configuration. Each root module is located using the given When all modules have been enumerated then a readability graph is computed, and in conjunction with the module exports and service use, checked for consistency. Resolution may fail with
Resolution may fail with
Implementation Note In the implementation then observability of modules may depend on referential integrity or other checks that ensure different builds of tightly coupled modules or modules for specific operating systems or architectures are not combined in the same configuration.
| |
| resolveAndBind | back to summary |
|---|---|
| public Configuration resolveAndBind(ModuleFinder before, ModuleFinder after, Collection<String> roots) Resolves a collection of root modules, with service binding, and with
this configuration as its parent, to create a new configuration.
This method works exactly as specified by the static
| |
| resolveAndBind | back to summary |
|---|---|
| pack-priv static Configuration resolveAndBind(ModuleFinder finder, Collection<String> roots, PrintStream traceOutput) Resolves a collection of root modules, with service binding, and with the empty configuration as its parent. This method is used to create the configuration for the boot layer. | |
| resolveAndBind | back to summary |
|---|---|
| public static Configuration resolveAndBind(ModuleFinder before, List<Configuration> parents, ModuleFinder after, Collection<String> roots) Resolves a collection of root modules, with service binding, to create configuration. This method works exactly as specified by More specifically, the root modules are
resolved as if by calling As service binding involves resolution then it may fail with
| |
| targetPlatform | back to summary |
|---|---|
| pack-priv String targetPlatform() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a string describing this configuration. | |