Maybe it is simple question but still can find how to do it. Let's suppose I use the following code to create layer
ModuleFinder finder = ModuleFinder.of(moduleCPath);
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("module-c"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer myLayer = parent.defineModulesWithOneLoader(cf, scl);
As I understand for myLayer
one classloader was created. How can I get reference to this classloader having reference to myLayer
and without knowing what classes can be inside myLayer
modules?
When a ModuleLayer
is created each Module
gets mapped to a ClassLoader
.
ModuleLayer
A layer is created from a graph of modules in a Configuration and a function that maps each module to a ClassLoader.
A ModuleLayer
has no concept of a ClassLoader
because there is no guarantee that every Module
will have the same ClassLoader
. Instead, the ClassLoader
is associated with each individual Module
, which you can get via Module.getClassLoader()
.
Since you are using ModuleLayer.defineModulesWithOneLoader(Configuration,ClassLoader)
1, however, there is a guarantee that each Module
will have the same ClassLoader
. This means you can get the ClassLoader
from any Module
in the ModuleLayer
and consider it as the "ClassLoader
of the ModuleLayer
".
ModuleLayer layer = parent.defineModulesWithOneLoader(...);
ClassLoader loader = layer.modules().iterator().next().getClassLoader();
Note that this won't work as intended if:
ModuleLayer
is empty (has no Module
s).
iterator().next()
will throw a NoSuchElementException
.ModuleLayer
via defineModulesWithManyLoaders(Configuration,ClassLoader)
1
Module
will have its own ClassLoader
.ModuleLayer
via defineModules(Configuration,Function)
and the Function
doesn't return the same ClassLoader
every time.
Module
to ClassLoader
. Also, each ClassLoader
can potentially have different parents. This method provides the most flexibility when defining ModuleLayer
s.1. The methods defineModulesWithOneLoader
and defineModulesWithManyLoaders
are, in effect, convenience methods for defineModules
for two pre-definable use cases; each Module
has the same ClassLoader
and each Module
has their own ClassLoader
, respectively.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments