use of java.lang.module.ResolvedModule in project junit5 by junit-team.
the class ModuleUtils method streamResolvedModules.
/**
* Stream resolved modules from current (or boot) module layer.
*/
private static Stream<ResolvedModule> streamResolvedModules(Predicate<String> moduleNamePredicate) {
Module module = ModuleUtils.class.getModule();
ModuleLayer layer = module.getLayer();
if (layer == null) {
logger.config(() -> ModuleUtils.class + " is a member of " + module + " - using boot layer returned by ModuleLayer.boot() as fall-back.");
layer = ModuleLayer.boot();
}
return streamResolvedModules(moduleNamePredicate, layer);
}
use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.
the class LauncherHelper method showResolvedModules.
/**
* Called by the launcher to show the resolved modules
*/
static void showResolvedModules() {
initOutput(System.out);
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration cf = bootLayer.configuration();
cf.modules().stream().map(ResolvedModule::reference).sorted(new JrtFirstComparator()).forEach(LauncherHelper::showModule);
}
use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.
the class Module method findModule.
/**
* Find the runtime Module corresponding to the given ResolvedModule
* in the given parent layer (or its parents).
*/
private static Module findModule(ModuleLayer parent, ResolvedModule resolvedModule) {
Configuration cf = resolvedModule.configuration();
String dn = resolvedModule.name();
return parent.layers().filter(l -> l.configuration() == cf).findAny().map(layer -> {
Optional<Module> om = layer.findModule(dn);
assert om.isPresent() : dn + " not found in layer";
Module m = om.get();
assert m.getLayer() == layer : m + " not in expected layer";
return m;
}).orElse(null);
}
use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.
the class Module method defineModules.
// -- creating Module objects --
/**
* Defines all module in a configuration to the runtime.
*
* @return a map of module name to runtime {@code Module}
*
* @throws IllegalArgumentException
* If defining any of the modules to the VM fails
*/
static Map<String, Module> defineModules(Configuration cf, Function<String, ClassLoader> clf, ModuleLayer layer) {
boolean isBootLayer = (ModuleLayer.boot() == null);
int cap = (int) (cf.modules().size() / 0.75f + 1.0f);
Map<String, Module> nameToModule = new HashMap<>(cap);
Map<String, ClassLoader> nameToLoader = new HashMap<>(cap);
Set<ClassLoader> loaders = new HashSet<>();
boolean hasPlatformModules = false;
// map each module to a class loader
for (ResolvedModule resolvedModule : cf.modules()) {
String name = resolvedModule.name();
ClassLoader loader = clf.apply(name);
nameToLoader.put(name, loader);
if (loader == null || loader == ClassLoaders.platformClassLoader()) {
if (!(clf instanceof ModuleLoaderMap.Mapper)) {
throw new IllegalArgumentException("loader can't be 'null'" + " or the platform class loader");
}
hasPlatformModules = true;
} else {
loaders.add(loader);
}
}
// define each module in the configuration to the VM
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
String name = descriptor.name();
ClassLoader loader = nameToLoader.get(name);
Module m;
if (loader == null && name.equals("java.base")) {
// java.base is already defined to the VM
m = Object.class.getModule();
} else {
URI uri = mref.location().orElse(null);
m = new Module(layer, loader, descriptor, uri);
}
nameToModule.put(name, m);
}
// setup readability and exports/opens
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
String mn = descriptor.name();
Module m = nameToModule.get(mn);
assert m != null;
// reads
Set<Module> reads = new HashSet<>();
// name -> source Module when in parent layer
Map<String, Module> nameToSource = Collections.emptyMap();
for (ResolvedModule other : resolvedModule.reads()) {
Module m2 = null;
if (other.configuration() == cf) {
// this configuration
m2 = nameToModule.get(other.name());
assert m2 != null;
} else {
// parent layer
for (ModuleLayer parent : layer.parents()) {
m2 = findModule(parent, other);
if (m2 != null)
break;
}
assert m2 != null;
if (nameToSource.isEmpty())
nameToSource = new HashMap<>();
nameToSource.put(other.name(), m2);
}
reads.add(m2);
// update VM view
addReads0(m, m2);
}
m.reads = reads;
// automatic modules read all unnamed modules
if (descriptor.isAutomatic()) {
m.implAddReads(ALL_UNNAMED_MODULE, true);
}
// exports and opens, skipped for open and automatic
if (!descriptor.isOpen() && !descriptor.isAutomatic()) {
if (isBootLayer && descriptor.opens().isEmpty()) {
// no open packages, no qualified exports to modules in parent layers
initExports(m, nameToModule);
} else {
initExportsAndOpens(m, nameToSource, nameToModule, layer.parents());
}
}
}
// then register the modules in the class loader's services catalog
if (hasPlatformModules) {
ClassLoader pcl = ClassLoaders.platformClassLoader();
ServicesCatalog bootCatalog = BootLoader.getServicesCatalog();
ServicesCatalog pclCatalog = ServicesCatalog.getServicesCatalog(pcl);
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleReference mref = resolvedModule.reference();
ModuleDescriptor descriptor = mref.descriptor();
if (!descriptor.provides().isEmpty()) {
String name = descriptor.name();
Module m = nameToModule.get(name);
ClassLoader loader = nameToLoader.get(name);
if (loader == null) {
bootCatalog.register(m);
} else if (loader == pcl) {
pclCatalog.register(m);
}
}
}
}
// record that there is a layer with modules defined to the class loader
for (ClassLoader loader : loaders) {
layer.bindToLoader(loader);
}
return nameToModule;
}
use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.
the class ModuleLayer method checkForDuplicatePkgs.
/**
* 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.
*
* @throws LayerInstantiationException
*/
private static void checkForDuplicatePkgs(Configuration cf, Function<String, ClassLoader> clf) {
// HashMap allows null keys
Map<ClassLoader, Set<String>> loaderToPackages = new HashMap<>();
for (ResolvedModule resolvedModule : cf.modules()) {
ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
ClassLoader loader = clf.apply(descriptor.name());
Set<String> loaderPackages = loaderToPackages.computeIfAbsent(loader, k -> new HashSet<>());
for (String pkg : descriptor.packages()) {
boolean added = loaderPackages.add(pkg);
if (!added) {
throw fail("More than one module with package %s mapped" + " to the same class loader", pkg);
}
}
}
}
Aggregations