Search in sources :

Example 16 with ModuleReference

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

the class Loader method findResourcesAsList.

/**
 * Finds the resources with the given name in this class loader.
 */
private List<URL> findResourcesAsList(String name) throws IOException {
    String pn = Resources.toPackageName(name);
    LoadedModule module = localPackageToModule.get(pn);
    if (module != null) {
        URL url = findResource(module.name(), name);
        if (url != null && (name.endsWith(".class") || url.toString().endsWith("/") || isOpen(module.mref(), pn))) {
            return List.of(url);
        } else {
            return Collections.emptyList();
        }
    } else {
        List<URL> urls = new ArrayList<>();
        for (ModuleReference mref : nameToModule.values()) {
            URL url = findResource(mref.descriptor().name(), name);
            if (url != null) {
                urls.add(url);
            }
        }
        return urls;
    }
}
Also used : ModuleReference(java.lang.module.ModuleReference) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 17 with ModuleReference

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

the class LauncherHelper method describeModule.

/**
 * Called by the launcher to describe a module
 */
static void describeModule(String moduleName) {
    initOutput(System.out);
    ModuleFinder finder = ModuleBootstrap.limitedFinder();
    ModuleReference mref = finder.find(moduleName).orElse(null);
    if (mref == null) {
        abort(null, "java.launcher.module.error4", moduleName);
    }
    ModuleDescriptor md = mref.descriptor();
    // one-line summary
    showModule(mref);
    // unqualified exports (sorted by package)
    md.exports().stream().filter(e -> !e.isQualified()).sorted(Comparator.comparing(Exports::source)).map(e -> Stream.concat(Stream.of(e.source()), toStringStream(e.modifiers())).collect(Collectors.joining(" "))).forEach(sourceAndMods -> ostream.format("exports %s%n", sourceAndMods));
    // dependences
    for (Requires r : md.requires()) {
        String nameAndMods = Stream.concat(Stream.of(r.name()), toStringStream(r.modifiers())).collect(Collectors.joining(" "));
        ostream.format("requires %s", nameAndMods);
        finder.find(r.name()).map(ModuleReference::descriptor).filter(ModuleDescriptor::isAutomatic).ifPresent(any -> ostream.print(" automatic"));
        ostream.println();
    }
    // service use and provides
    for (String s : md.uses()) {
        ostream.format("uses %s%n", s);
    }
    for (Provides ps : md.provides()) {
        String names = ps.providers().stream().collect(Collectors.joining(" "));
        ostream.format("provides %s with %s%n", ps.service(), names);
    }
    // qualified exports
    for (Exports e : md.exports()) {
        if (e.isQualified()) {
            String who = e.targets().stream().collect(Collectors.joining(" "));
            ostream.format("qualified exports %s to %s%n", e.source(), who);
        }
    }
    // open packages
    for (Opens opens : md.opens()) {
        if (opens.isQualified())
            ostream.print("qualified ");
        String sourceAndMods = Stream.concat(Stream.of(opens.source()), toStringStream(opens.modifiers())).collect(Collectors.joining(" "));
        ostream.format("opens %s", sourceAndMods);
        if (opens.isQualified()) {
            String who = opens.targets().stream().collect(Collectors.joining(" "));
            ostream.format(" to %s", who);
        }
        ostream.println();
    }
    // non-exported/non-open packages
    Set<String> concealed = new TreeSet<>(md.packages());
    md.exports().stream().map(Exports::source).forEach(concealed::remove);
    md.opens().stream().map(Opens::source).forEach(concealed::remove);
    concealed.forEach(p -> ostream.format("contains %s%n", p));
}
Also used : Manifest(java.util.jar.Manifest) NoSuchFileException(java.nio.file.NoSuchFileException) ModuleReference(java.lang.module.ModuleReference) JarFile(java.util.jar.JarFile) Requires(java.lang.module.ModuleDescriptor.Requires) BigDecimal(java.math.BigDecimal) DirectoryStream(java.nio.file.DirectoryStream) Category(java.util.Locale.Category) ModuleFinder(java.lang.module.ModuleFinder) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) Method(java.lang.reflect.Method) Path(java.nio.file.Path) FindException(java.lang.module.FindException) RoundingMode(java.math.RoundingMode) Set(java.util.Set) Configuration(java.lang.module.Configuration) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Exports(java.lang.module.ModuleDescriptor.Exports) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) VM(jdk.internal.misc.VM) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Provides(java.lang.module.ModuleDescriptor.Provides) ModuleDescriptor(java.lang.module.ModuleDescriptor) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) ResolvedModule(java.lang.module.ResolvedModule) Charset(java.nio.charset.Charset) ResourceBundle(java.util.ResourceBundle) PrintStream(java.io.PrintStream) Properties(java.util.Properties) Iterator(java.util.Iterator) Files(java.nio.file.Files) Opens(java.lang.module.ModuleDescriptor.Opens) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) Normalizer(java.text.Normalizer) ModuleBootstrap(jdk.internal.module.ModuleBootstrap) Paths(java.nio.file.Paths) Modules(jdk.internal.module.Modules) Comparator(java.util.Comparator) Collections(java.util.Collections) ModuleDescriptor(java.lang.module.ModuleDescriptor) ModuleFinder(java.lang.module.ModuleFinder) Opens(java.lang.module.ModuleDescriptor.Opens) ModuleReference(java.lang.module.ModuleReference) Requires(java.lang.module.ModuleDescriptor.Requires) TreeSet(java.util.TreeSet) Provides(java.lang.module.ModuleDescriptor.Provides) Exports(java.lang.module.ModuleDescriptor.Exports)

Example 18 with ModuleReference

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

the class LauncherHelper method showModule.

/**
 * Prints a single line with the module name, version and modifiers
 */
private static void showModule(ModuleReference mref) {
    ModuleDescriptor md = mref.descriptor();
    ostream.print(md.toNameAndVersion());
    mref.location().filter(uri -> !isJrt(uri)).ifPresent(uri -> ostream.format(" %s", uri));
    if (md.isOpen())
        ostream.print(" open");
    if (md.isAutomatic())
        ostream.print(" automatic");
    ostream.println();
}
Also used : Manifest(java.util.jar.Manifest) NoSuchFileException(java.nio.file.NoSuchFileException) ModuleReference(java.lang.module.ModuleReference) JarFile(java.util.jar.JarFile) Requires(java.lang.module.ModuleDescriptor.Requires) BigDecimal(java.math.BigDecimal) DirectoryStream(java.nio.file.DirectoryStream) Category(java.util.Locale.Category) ModuleFinder(java.lang.module.ModuleFinder) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) Method(java.lang.reflect.Method) Path(java.nio.file.Path) FindException(java.lang.module.FindException) RoundingMode(java.math.RoundingMode) Set(java.util.Set) Configuration(java.lang.module.Configuration) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Exports(java.lang.module.ModuleDescriptor.Exports) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) VM(jdk.internal.misc.VM) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Provides(java.lang.module.ModuleDescriptor.Provides) ModuleDescriptor(java.lang.module.ModuleDescriptor) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) ResolvedModule(java.lang.module.ResolvedModule) Charset(java.nio.charset.Charset) ResourceBundle(java.util.ResourceBundle) PrintStream(java.io.PrintStream) Properties(java.util.Properties) Iterator(java.util.Iterator) Files(java.nio.file.Files) Opens(java.lang.module.ModuleDescriptor.Opens) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) Normalizer(java.text.Normalizer) ModuleBootstrap(jdk.internal.module.ModuleBootstrap) Paths(java.nio.file.Paths) Modules(jdk.internal.module.Modules) Comparator(java.util.Comparator) Collections(java.util.Collections) ModuleDescriptor(java.lang.module.ModuleDescriptor)

Example 19 with ModuleReference

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

the class NamedPackage method location.

/**
 * Returns the location of the module if this named package is in
 * a named module; otherwise, returns null.
 */
URI location() {
    if (module.isNamed() && module.getLayer() != null) {
        Configuration cf = module.getLayer().configuration();
        ModuleReference mref = cf.findModule(module.getName()).get().reference();
        return mref.location().orElse(null);
    }
    return null;
}
Also used : Configuration(java.lang.module.Configuration) ModuleReference(java.lang.module.ModuleReference)

Example 20 with ModuleReference

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

the class ModuleBootstrap method createBootLayerForValidation.

/**
 * Create a boot module layer for validation that resolves all
 * system modules.
 */
private static ModuleLayer createBootLayerForValidation() {
    Set<String> allSystem = ModuleFinder.ofSystem().findAll().stream().map(ModuleReference::descriptor).map(ModuleDescriptor::name).collect(Collectors.toSet());
    Configuration cf = SharedSecrets.getJavaLangModuleAccess().resolveAndBind(ModuleFinder.ofSystem(), allSystem, null);
    Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
    return ModuleLayer.empty().defineModules(cf, clf);
}
Also used : Configuration(java.lang.module.Configuration) 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