Search in sources :

Example 16 with ModuleDescriptor

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

the class SystemModuleFinders method of.

/**
 * Returns a ModuleFinder that is backed by the given SystemModules object.
 *
 * @apiNote The returned ModuleFinder is thread safe.
 */
static ModuleFinder of(SystemModules systemModules) {
    ModuleDescriptor[] descriptors = systemModules.moduleDescriptors();
    ModuleTarget[] targets = systemModules.moduleTargets();
    ModuleHashes[] recordedHashes = systemModules.moduleHashes();
    ModuleResolution[] moduleResolutions = systemModules.moduleResolutions();
    int moduleCount = descriptors.length;
    ModuleReference[] mrefs = new ModuleReference[moduleCount];
    @SuppressWarnings(value = { "rawtypes", "unchecked" }) Map.Entry<String, ModuleReference>[] map = (Map.Entry<String, ModuleReference>[]) new Map.Entry[moduleCount];
    Map<String, byte[]> nameToHash = generateNameToHash(recordedHashes);
    for (int i = 0; i < moduleCount; i++) {
        String name = descriptors[i].name();
        HashSupplier hashSupplier = hashSupplier(nameToHash, name);
        ModuleReference mref = toModuleReference(descriptors[i], targets[i], recordedHashes[i], hashSupplier, moduleResolutions[i]);
        mrefs[i] = mref;
        map[i] = Map.entry(name, mref);
    }
    return new SystemModuleFinder(mrefs, map);
}
Also used : HashSupplier(jdk.internal.module.ModuleHashes.HashSupplier) ModuleDescriptor(java.lang.module.ModuleDescriptor) ModuleReference(java.lang.module.ModuleReference) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with ModuleDescriptor

use of java.lang.module.ModuleDescriptor 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 18 with ModuleDescriptor

use of java.lang.module.ModuleDescriptor 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 19 with ModuleDescriptor

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

Aggregations

ModuleDescriptor (java.lang.module.ModuleDescriptor)19 HashMap (java.util.HashMap)12 HashSet (java.util.HashSet)11 ModuleReference (java.lang.module.ModuleReference)10 ResolvedModule (java.lang.module.ResolvedModule)9 Set (java.util.Set)9 Map (java.util.Map)8 URI (java.net.URI)7 ArrayList (java.util.ArrayList)7 File (java.io.File)6 IOException (java.io.IOException)6 Collections (java.util.Collections)6 List (java.util.List)6 Optional (java.util.Optional)6 UncheckedIOException (java.io.UncheckedIOException)5 ModuleFinder (java.lang.module.ModuleFinder)5 Path (java.nio.file.Path)5 Paths (java.nio.file.Paths)5 JarFile (java.util.jar.JarFile)5 Collectors (java.util.stream.Collectors)5