Search in sources :

Example 26 with ModuleReference

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

the class DefaultRoots method compute.

/**
 * Returns the default set of root modules for the unnamed module computed from
 * the system modules observable with the given module finder.
 */
static Set<String> compute(ModuleFinder systemModuleFinder, ModuleFinder finder) {
    Set<String> roots = new HashSet<>();
    boolean hasJava = false;
    if (systemModuleFinder.find(JAVA_SE).isPresent()) {
        if (finder == systemModuleFinder || finder.find(JAVA_SE).isPresent()) {
            // java.se is a system module
            hasJava = true;
            roots.add(JAVA_SE);
        }
    }
    for (ModuleReference mref : systemModuleFinder.findAll()) {
        String mn = mref.descriptor().name();
        if (hasJava && mn.startsWith("java.")) {
            // not a root
            continue;
        }
        if (ModuleResolution.doNotResolveByDefault(mref)) {
            // not a root
            continue;
        }
        if ((finder == systemModuleFinder || finder.find(mn).isPresent())) {
            // add as root if exports at least one package to all modules
            ModuleDescriptor descriptor = mref.descriptor();
            for (ModuleDescriptor.Exports e : descriptor.exports()) {
                if (!e.isQualified()) {
                    roots.add(mn);
                    break;
                }
            }
        }
    }
    return roots;
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) ModuleReference(java.lang.module.ModuleReference) HashSet(java.util.HashSet)

Example 27 with ModuleReference

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

the class IllegalAccessMaps method generate.

/**
 * Generate the maps of module to concealed and exported packages for
 * the system modules that are observable with the given module finder.
 */
public static IllegalAccessMaps generate(ModuleFinder finder) {
    Map<String, ModuleDescriptor> map = new HashMap<>();
    finder.findAll().stream().map(ModuleReference::descriptor).forEach(md -> md.packages().forEach(pn -> map.putIfAbsent(pn, md)));
    Map<String, Set<String>> concealedPackagesToOpen = new HashMap<>();
    Map<String, Set<String>> exportedPackagesToOpen = new HashMap<>();
    String rn = "jdk8_packages.dat";
    InputStream in = IllegalAccessMaps.class.getResourceAsStream(rn);
    if (in == null) {
        throw new InternalError(rn + " not found");
    }
    try (BufferedReader br = new BufferedReader(new InputStreamReader(in, UTF_8))) {
        br.lines().filter(line -> !line.isEmpty() && !line.startsWith("#")).forEach(pn -> {
            ModuleDescriptor descriptor = map.get(pn);
            if (descriptor != null && !isOpen(descriptor, pn)) {
                String name = descriptor.name();
                if (isExported(descriptor, pn)) {
                    exportedPackagesToOpen.computeIfAbsent(name, k -> new HashSet<>()).add(pn);
                } else {
                    concealedPackagesToOpen.computeIfAbsent(name, k -> new HashSet<>()).add(pn);
                }
            }
        });
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
    return new IllegalAccessMaps(concealedPackagesToOpen, exportedPackagesToOpen);
}
Also used : ModuleDescriptor(java.lang.module.ModuleDescriptor) Set(java.util.Set) IOException(java.io.IOException) ModuleReference(java.lang.module.ModuleReference) HashMap(java.util.HashMap) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet) ModuleFinder(java.lang.module.ModuleFinder) Map(java.util.Map) BufferedReader(java.io.BufferedReader) InputStream(java.io.InputStream) Set(java.util.Set) HashSet(java.util.HashSet) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ModuleDescriptor(java.lang.module.ModuleDescriptor) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Example 28 with ModuleReference

use of java.lang.module.ModuleReference 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)

Aggregations

ModuleReference (java.lang.module.ModuleReference)28 HashMap (java.util.HashMap)11 IOException (java.io.IOException)10 URI (java.net.URI)10 Path (java.nio.file.Path)10 Map (java.util.Map)10 ModuleDescriptor (java.lang.module.ModuleDescriptor)9 HashSet (java.util.HashSet)8 List (java.util.List)8 Configuration (java.lang.module.Configuration)7 ModuleFinder (java.lang.module.ModuleFinder)7 ArrayList (java.util.ArrayList)7 Optional (java.util.Optional)7 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 File (java.io.File)6 UncheckedIOException (java.io.UncheckedIOException)6 ModuleReader (java.lang.module.ModuleReader)6 ResolvedModule (java.lang.module.ResolvedModule)6 URL (java.net.URL)5