Search in sources :

Example 1 with ResolvedModule

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);
}
Also used : ResolvedModule(java.lang.module.ResolvedModule)

Example 2 with ResolvedModule

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);
}
Also used : Configuration(java.lang.module.Configuration) ResolvedModule(java.lang.module.ResolvedModule)

Example 3 with ResolvedModule

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);
}
Also used : ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter) URL(java.net.URL) ClassLoaders(jdk.internal.loader.ClassLoaders) ModuleDescriptor(java.lang.module.ModuleDescriptor) ModuleLoaderMap(jdk.internal.module.ModuleLoaderMap) ModuleReference(java.lang.module.ModuleReference) HashMap(java.util.HashMap) Function(java.util.function.Function) JavaLangAccess(jdk.internal.misc.JavaLangAccess) HashSet(java.util.HashSet) Reflection(jdk.internal.reflect.Reflection) Opcodes(jdk.internal.org.objectweb.asm.Opcodes) Version(java.lang.module.ModuleDescriptor.Version) ResolvedModule(java.lang.module.ResolvedModule) Map(java.util.Map) SecurityConstants(sun.security.util.SecurityConstants) URI(java.net.URI) Resources(jdk.internal.module.Resources) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) Iterator(java.util.Iterator) SharedSecrets(jdk.internal.misc.SharedSecrets) Opens(java.lang.module.ModuleDescriptor.Opens) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Attribute(jdk.internal.org.objectweb.asm.Attribute) Set(java.util.Set) IOException(java.io.IOException) Configuration(java.lang.module.Configuration) PrivilegedAction(java.security.PrivilegedAction) IllegalAccessLogger(jdk.internal.module.IllegalAccessLogger) ServicesCatalog(jdk.internal.module.ServicesCatalog) AnnotationVisitor(jdk.internal.org.objectweb.asm.AnnotationVisitor) Collectors(java.util.stream.Collectors) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) Objects(java.util.Objects) Exports(java.lang.module.ModuleDescriptor.Exports) BootLoader(jdk.internal.loader.BootLoader) List(java.util.List) Stream(java.util.stream.Stream) CallerSensitive(jdk.internal.reflect.CallerSensitive) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) AccessController(java.security.AccessController) Collections(java.util.Collections) InputStream(java.io.InputStream) AnnotatedElement(java.lang.reflect.AnnotatedElement) Configuration(java.lang.module.Configuration) Optional(java.util.Optional) ResolvedModule(java.lang.module.ResolvedModule)

Example 4 with ResolvedModule

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;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServicesCatalog(jdk.internal.module.ServicesCatalog) URI(java.net.URI) ModuleDescriptor(java.lang.module.ModuleDescriptor) ResolvedModule(java.lang.module.ResolvedModule) ModuleReference(java.lang.module.ModuleReference) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ResolvedModule(java.lang.module.ResolvedModule) HashSet(java.util.HashSet)

Example 5 with ResolvedModule

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);
            }
        }
    }
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) Set(java.util.Set) HashSet(java.util.HashSet) ResolvedModule(java.lang.module.ResolvedModule) HashMap(java.util.HashMap)

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