Search in sources :

Example 6 with ModuleFinder

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

Aggregations

ModuleFinder (java.lang.module.ModuleFinder)6 ModuleReference (java.lang.module.ModuleReference)6 Set (java.util.Set)5 Configuration (java.lang.module.Configuration)4 ModuleDescriptor (java.lang.module.ModuleDescriptor)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ResolvedModule (java.lang.module.ResolvedModule)3 URI (java.net.URI)3 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3 File (java.io.File)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 Paths (java.nio.file.Paths)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Optional (java.util.Optional)2