Search in sources :

Example 6 with ModuleReference

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

the class ModulePath method find.

@Override
public Optional<ModuleReference> find(String name) {
    Objects.requireNonNull(name);
    // try cached modules
    ModuleReference m = cachedModules.get(name);
    if (m != null)
        return Optional.of(m);
    // the module may not have been encountered yet
    while (hasNextEntry()) {
        scanNextEntry();
        m = cachedModules.get(name);
        if (m != null)
            return Optional.of(m);
    }
    return Optional.empty();
}
Also used : ModuleReference(java.lang.module.ModuleReference)

Example 7 with ModuleReference

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

the class ModuleReferences method newJModModule.

/**
 * Creates a ModuleReference to a module in a JMOD file.
 */
static ModuleReference newJModModule(ModuleInfo.Attributes attrs, Path file) {
    URI uri = file.toUri();
    Supplier<ModuleReader> supplier = () -> new JModModuleReader(file, uri);
    HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a);
    return newModule(attrs, uri, supplier, null, hasher);
}
Also used : VersionedStream(jdk.internal.util.jar.VersionedStream) ModuleReference(java.lang.module.ModuleReference) JarFile(java.util.jar.JarFile) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ModuleReader(java.lang.module.ModuleReader) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) JarEntry(java.util.jar.JarEntry) ZipFile(java.util.zip.ZipFile) URI(java.net.URI) Path(java.nio.file.Path) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) SharedSecrets(jdk.internal.misc.SharedSecrets) HashSupplier(jdk.internal.module.ModuleHashes.HashSupplier) Files(java.nio.file.Files) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) IOError(java.io.IOError) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Lock(java.util.concurrent.locks.Lock) JmodFile(jdk.internal.jmod.JmodFile) Stream(java.util.stream.Stream) Optional(java.util.Optional) ParseUtil(sun.net.www.ParseUtil) InputStream(java.io.InputStream) ModuleReader(java.lang.module.ModuleReader) HashSupplier(jdk.internal.module.ModuleHashes.HashSupplier) URI(java.net.URI)

Example 8 with ModuleReference

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

the class Modules method loadModule.

/**
 * Called by the VM to load a system module, typically "java.instrument" or
 * "jdk.management.agent". If the module is not loaded then it is resolved
 * and loaded (along with any dependences that weren't previously loaded)
 * into a child layer.
 */
public static synchronized Module loadModule(String name) {
    ModuleLayer top = topLayer;
    if (top == null)
        top = ModuleLayer.boot();
    Module module = top.findModule(name).orElse(null);
    if (module != null) {
        // module already loaded
        return module;
    }
    // resolve the module with the top-most layer as the parent
    ModuleFinder empty = ModuleFinder.of();
    ModuleFinder finder = ModuleBootstrap.unlimitedFinder();
    Set<String> roots = Set.of(name);
    Configuration cf = top.configuration().resolveAndBind(empty, finder, roots);
    // create the child layer
    Function<String, ClassLoader> clf = ModuleLoaderMap.mappingFunction(cf);
    ModuleLayer newLayer = top.defineModules(cf, clf);
    // add qualified exports/opens to give access to modules in child layer
    Map<String, Module> map = newLayer.modules().stream().collect(Collectors.toMap(Module::getName, Function.identity()));
    ModuleLayer layer = top;
    while (layer != null) {
        for (Module m : layer.modules()) {
            // qualified exports
            m.getDescriptor().exports().stream().filter(ModuleDescriptor.Exports::isQualified).forEach(e -> e.targets().forEach(target -> {
                Module other = map.get(target);
                if (other != null) {
                    addExports(m, e.source(), other);
                }
            }));
            // qualified opens
            m.getDescriptor().opens().stream().filter(ModuleDescriptor.Opens::isQualified).forEach(o -> o.targets().forEach(target -> {
                Module other = map.get(target);
                if (other != null) {
                    addOpens(m, o.source(), other);
                }
            }));
        }
        List<ModuleLayer> parents = layer.parents();
        assert parents.size() <= 1;
        layer = parents.isEmpty() ? null : parents.get(0);
    }
    // update security manager before making types visible
    JLA.addNonExportedPackages(newLayer);
    // update the built-in class loaders to make the types visible
    for (ResolvedModule resolvedModule : cf.modules()) {
        ModuleReference mref = resolvedModule.reference();
        String mn = mref.descriptor().name();
        ClassLoader cl = clf.apply(mn);
        if (cl == null) {
            BootLoader.loadModule(mref);
        } else {
            ((BuiltinClassLoader) cl).loadModule(mref);
        }
    }
    // new top layer
    topLayer = newLayer;
    // return module
    return newLayer.findModule(name).orElseThrow(() -> new InternalError("module not loaded"));
}
Also used : SharedSecrets(jdk.internal.misc.SharedSecrets) ClassLoaders(jdk.internal.loader.ClassLoaders) ModuleDescriptor(java.lang.module.ModuleDescriptor) Set(java.util.Set) Configuration(java.lang.module.Configuration) ModuleReference(java.lang.module.ModuleReference) PrivilegedAction(java.security.PrivilegedAction) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) JavaLangAccess(jdk.internal.misc.JavaLangAccess) BootLoader(jdk.internal.loader.BootLoader) List(java.util.List) ResolvedModule(java.lang.module.ResolvedModule) ModuleFinder(java.lang.module.ModuleFinder) Map(java.util.Map) URI(java.net.URI) AccessController(java.security.AccessController) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ModuleFinder(java.lang.module.ModuleFinder) Configuration(java.lang.module.Configuration) 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)

Example 9 with ModuleReference

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

the class SystemModuleFinders method ofModuleInfos.

/**
 * Parses the module-info.class of all module in the runtime image and
 * returns a ModuleFinder to find the modules.
 *
 * @apiNote The returned ModuleFinder is thread safe.
 */
private static ModuleFinder ofModuleInfos() {
    // parse the module-info.class in every module
    Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
    Map<String, byte[]> nameToHash = new HashMap<>();
    ImageReader reader = SystemImage.reader();
    for (String mn : reader.getModuleNames()) {
        ImageLocation loc = reader.findLocation(mn, "module-info.class");
        ModuleInfo.Attributes attrs = ModuleInfo.read(reader.getResourceBuffer(loc), null);
        nameToAttributes.put(mn, attrs);
        ModuleHashes hashes = attrs.recordedHashes();
        if (hashes != null) {
            for (String name : hashes.names()) {
                nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
            }
        }
    }
    // create a ModuleReference for each module
    Set<ModuleReference> mrefs = new HashSet<>();
    Map<String, ModuleReference> nameToModule = new HashMap<>();
    for (Map.Entry<String, ModuleInfo.Attributes> e : nameToAttributes.entrySet()) {
        String mn = e.getKey();
        ModuleInfo.Attributes attrs = e.getValue();
        HashSupplier hashSupplier = hashSupplier(nameToHash, mn);
        ModuleReference mref = toModuleReference(attrs.descriptor(), attrs.target(), attrs.recordedHashes(), hashSupplier, attrs.moduleResolution());
        mrefs.add(mref);
        nameToModule.put(mn, mref);
    }
    return new SystemModuleFinder(mrefs, nameToModule);
}
Also used : HashMap(java.util.HashMap) HashSupplier(jdk.internal.module.ModuleHashes.HashSupplier) ImageLocation(jdk.internal.jimage.ImageLocation) ModuleReference(java.lang.module.ModuleReference) ImageReader(jdk.internal.jimage.ImageReader) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 10 with ModuleReference

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

the class SystemModuleFinders method ofSystem.

/**
 * Returns the ModuleFinder to find all system modules. Supports both
 * images and exploded builds.
 *
 * @apiNote Used by ModuleFinder.ofSystem()
 */
public static ModuleFinder ofSystem() {
    ModuleFinder finder = cachedSystemModuleFinder;
    if (finder != null) {
        return finder;
    }
    // probe to see if this is an images build
    String home = System.getProperty("java.home");
    Path modules = Paths.get(home, "lib", "modules");
    if (Files.isRegularFile(modules)) {
        if (USE_FAST_PATH) {
            SystemModules systemModules = allSystemModules();
            if (systemModules != null) {
                finder = of(systemModules);
            }
        }
        // fall back to parsing the module-info.class files in image
        if (finder == null) {
            finder = ofModuleInfos();
        }
        cachedSystemModuleFinder = finder;
        return finder;
    }
    // exploded build (do not cache module finder)
    Path dir = Paths.get(home, "modules");
    if (!Files.isDirectory(dir))
        throw new InternalError("Unable to detect the run-time image");
    ModuleFinder f = ModulePath.of(ModuleBootstrap.patcher(), dir);
    return new ModuleFinder() {

        @Override
        public Optional<ModuleReference> find(String name) {
            PrivilegedAction<Optional<ModuleReference>> pa = () -> f.find(name);
            return AccessController.doPrivileged(pa);
        }

        @Override
        public Set<ModuleReference> findAll() {
            PrivilegedAction<Set<ModuleReference>> pa = f::findAll;
            return AccessController.doPrivileged(pa);
        }
    };
}
Also used : Path(java.nio.file.Path) ModuleFinder(java.lang.module.ModuleFinder) HashSet(java.util.HashSet) Set(java.util.Set) Optional(java.util.Optional) ModuleReference(java.lang.module.ModuleReference)

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