Search in sources :

Example 11 with ResolvedModule

use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.

the class ModuleLoaderMap method mappingFunction.

/**
 * Returns the function to map modules in the given configuration to the
 * built-in class loaders.
 */
static Function<String, ClassLoader> mappingFunction(Configuration cf) {
    Set<String> bootModules = bootModules();
    Set<String> platformModules = platformModules();
    ClassLoader platformClassLoader = ClassLoaders.platformClassLoader();
    ClassLoader appClassLoader = ClassLoaders.appClassLoader();
    Map<String, ClassLoader> map = new HashMap<>();
    for (ResolvedModule resolvedModule : cf.modules()) {
        String mn = resolvedModule.name();
        if (!bootModules.contains(mn)) {
            if (platformModules.contains(mn)) {
                map.put(mn, platformClassLoader);
            } else {
                map.put(mn, appClassLoader);
            }
        }
    }
    return new Mapper(map);
}
Also used : ResolvedModule(java.lang.module.ResolvedModule) HashMap(java.util.HashMap)

Example 12 with ResolvedModule

use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.

the class ModuleBootstrap method checkSplitPackages.

/**
 * Checks for split packages between modules defined to the built-in class
 * loaders.
 */
private static void checkSplitPackages(Configuration cf, Function<String, ClassLoader> clf) {
    Map<String, String> packageToModule = new HashMap<>();
    for (ResolvedModule resolvedModule : cf.modules()) {
        ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
        String name = descriptor.name();
        ClassLoader loader = clf.apply(name);
        if (loader == null || loader instanceof BuiltinClassLoader) {
            for (String p : descriptor.packages()) {
                String other = packageToModule.putIfAbsent(p, name);
                if (other != null) {
                    String msg = "Package " + p + " in both module " + name + " and module " + other;
                    throw new LayerInstantiationException(msg);
                }
            }
        }
    }
}
Also used : BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ModuleDescriptor(java.lang.module.ModuleDescriptor) ResolvedModule(java.lang.module.ResolvedModule) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader)

Example 13 with ResolvedModule

use of java.lang.module.ResolvedModule in project Bytecoder by mirkosertic.

the class ModuleBootstrap method loadModules.

/**
 * Load/register the modules to the built-in class loaders.
 */
private static void loadModules(Configuration cf, Function<String, ClassLoader> clf) {
    for (ResolvedModule resolvedModule : cf.modules()) {
        ModuleReference mref = resolvedModule.reference();
        String name = resolvedModule.name();
        ClassLoader loader = clf.apply(name);
        if (loader == null) {
            // skip java.base as it is already loaded
            if (!name.equals(JAVA_BASE)) {
                BootLoader.loadModule(mref);
            }
        } else if (loader instanceof BuiltinClassLoader) {
            ((BuiltinClassLoader) loader).loadModule(mref);
        }
    }
}
Also used : BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ResolvedModule(java.lang.module.ResolvedModule) ModuleReference(java.lang.module.ModuleReference) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader)

Example 14 with ResolvedModule

use of java.lang.module.ResolvedModule in project junit5 by junit-team.

the class ModuleUtils method streamResolvedModules.

/**
 * Stream resolved modules from the supplied layer.
 */
private static Stream<ResolvedModule> streamResolvedModules(Predicate<String> moduleNamePredicate, ModuleLayer layer) {
    logger.debug(() -> "Streaming modules for layer @" + System.identityHashCode(layer) + ": " + layer);
    Configuration configuration = layer.configuration();
    logger.debug(() -> "Module layer configuration: " + configuration);
    Stream<ResolvedModule> stream = configuration.modules().stream();
    return stream.filter(module -> moduleNamePredicate.test(module.name()));
}
Also used : Configuration(java.lang.module.Configuration) ResolvedModule(java.lang.module.ResolvedModule)

Aggregations

ResolvedModule (java.lang.module.ResolvedModule)14 HashMap (java.util.HashMap)8 Configuration (java.lang.module.Configuration)7 ModuleDescriptor (java.lang.module.ModuleDescriptor)7 URI (java.net.URI)7 ModuleReference (java.lang.module.ModuleReference)6 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5 Collections (java.util.Collections)4 Set (java.util.Set)4 Path (java.nio.file.Path)3 AccessController (java.security.AccessController)3 PrivilegedAction (java.security.PrivilegedAction)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 BootLoader (jdk.internal.loader.BootLoader)3 JavaLangAccess (jdk.internal.misc.JavaLangAccess)3