use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class ModulePath method find.
@Override
public Optional<ModuleReference> find(String name) {
Objects.requireNonNull(name);
// try cached modules
ModuleReference m = cachedModules.get(name);
if (m != null)
return Optional.of(m);
// the module may not have been encountered yet
while (hasNextEntry()) {
scanNextEntry();
m = cachedModules.get(name);
if (m != null)
return Optional.of(m);
}
return Optional.empty();
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class ModuleReferences method newJModModule.
/**
* Creates a ModuleReference to a module in a JMOD file.
*/
static ModuleReference newJModModule(ModuleInfo.Attributes attrs, Path file) {
URI uri = file.toUri();
Supplier<ModuleReader> supplier = () -> new JModModuleReader(file, uri);
HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a);
return newModule(attrs, uri, supplier, null, hasher);
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class Modules method loadModule.
/**
* Called by the VM to load a system module, typically "java.instrument" or
* "jdk.management.agent". If the module is not loaded then it is resolved
* and loaded (along with any dependences that weren't previously loaded)
* into a child layer.
*/
public static synchronized Module loadModule(String name) {
ModuleLayer top = topLayer;
if (top == null)
top = ModuleLayer.boot();
Module module = top.findModule(name).orElse(null);
if (module != null) {
// module already loaded
return module;
}
// resolve the module with the top-most layer as the parent
ModuleFinder empty = ModuleFinder.of();
ModuleFinder finder = ModuleBootstrap.unlimitedFinder();
Set<String> roots = Set.of(name);
Configuration cf = top.configuration().resolveAndBind(empty, finder, roots);
// create the child layer
Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
ModuleLayer newLayer = top.defineModules(cf, clf);
// add qualified exports/opens to give access to modules in child layer
Map<String, Module> map = newLayer.modules().stream().collect(Collectors.toMap(Module::getName, Function.identity()));
ModuleLayer layer = top;
while (layer != null) {
for (Module m : layer.modules()) {
// qualified exports
m.getDescriptor().exports().stream().filter(ModuleDescriptor.Exports::isQualified).forEach(e -> e.targets().forEach(target -> {
Module other = map.get(target);
if (other != null) {
addExports(m, e.source(), other);
}
}));
// qualified opens
m.getDescriptor().opens().stream().filter(ModuleDescriptor.Opens::isQualified).forEach(o -> o.targets().forEach(target -> {
Module other = map.get(target);
if (other != null) {
addOpens(m, o.source(), other);
}
}));
}
List<ModuleLayer> parents = layer.parents();
assert parents.size() <= 1;
layer = parents.isEmpty() ? null : parents.get(0);
}
// update security manager before making types visible
JLA.addNonExportedPackages(newLayer);
// update the built-in class loaders to make the types visible
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
String mn = mref.descriptor().name();
ClassLoader cl = clf.apply(mn);
if (cl == null) {
BootLoader.loadModule(mref);
} else {
((BuiltinClassLoader) cl).loadModule(mref);
}
}
// new top layer
topLayer = newLayer;
// return module
return newLayer.findModule(name).orElseThrow(() -> new InternalError("module not loaded"));
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class SystemModuleFinders method ofModuleInfos.
/**
* Parses the module-info.class of all module in the runtime image and
* returns a ModuleFinder to find the modules.
*
* @apiNote The returned ModuleFinder is thread safe.
*/
private static ModuleFinder ofModuleInfos() {
// parse the module-info.class in every module
Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
Map<String, byte[]> nameToHash = new HashMap<>();
ImageReader reader = SystemImage.reader();
for (String mn : reader.getModuleNames()) {
ImageLocation loc = reader.findLocation(mn, "module-info.class");
ModuleInfo.Attributes attrs = ModuleInfo.read(reader.getResourceBuffer(loc), null);
nameToAttributes.put(mn, attrs);
ModuleHashes hashes = attrs.recordedHashes();
if (hashes != null) {
for (String name : hashes.names()) {
nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
}
}
}
// create a ModuleReference for each module
Set<ModuleReference> mrefs = new HashSet<>();
Map<String, ModuleReference> nameToModule = new HashMap<>();
for (Map.Entry<String, ModuleInfo.Attributes> e : nameToAttributes.entrySet()) {
String mn = e.getKey();
ModuleInfo.Attributes attrs = e.getValue();
HashSupplier hashSupplier = hashSupplier(nameToHash, mn);
ModuleReference mref = toModuleReference(attrs.descriptor(), attrs.target(), attrs.recordedHashes(), hashSupplier, attrs.moduleResolution());
mrefs.add(mref);
nameToModule.put(mn, mref);
}
return new SystemModuleFinder(mrefs, nameToModule);
}
use of java.lang.module.ModuleReference in project Bytecoder by mirkosertic.
the class SystemModuleFinders method ofSystem.
/**
* Returns the ModuleFinder to find all system modules. Supports both
* images and exploded builds.
*
* @apiNote Used by ModuleFinder.ofSystem()
*/
public static ModuleFinder ofSystem() {
ModuleFinder finder = cachedSystemModuleFinder;
if (finder != null) {
return finder;
}
// probe to see if this is an images build
String home = System.getProperty("java.home");
Path modules = Paths.get(home, "lib", "modules");
if (Files.isRegularFile(modules)) {
if (USE_FAST_PATH) {
SystemModules systemModules = allSystemModules();
if (systemModules != null) {
finder = of(systemModules);
}
}
// fall back to parsing the module-info.class files in image
if (finder == null) {
finder = ofModuleInfos();
}
cachedSystemModuleFinder = finder;
return finder;
}
// exploded build (do not cache module finder)
Path dir = Paths.get(home, "modules");
if (!Files.isDirectory(dir))
throw new InternalError("Unable to detect the run-time image");
ModuleFinder f = ModulePath.of(ModuleBootstrap.patcher(), dir);
return new ModuleFinder() {
@Override
public Optional<ModuleReference> find(String name) {
PrivilegedAction<Optional<ModuleReference>> pa = () -> f.find(name);
return AccessController.doPrivileged(pa);
}
@Override
public Set<ModuleReference> findAll() {
PrivilegedAction<Set<ModuleReference>> pa = f::findAll;
return AccessController.doPrivileged(pa);
}
};
}
Aggregations