Search in sources :

Example 1 with HashSupplier

use of jdk.internal.module.ModuleHashes.HashSupplier 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 2 with HashSupplier

use of jdk.internal.module.ModuleHashes.HashSupplier 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 3 with HashSupplier

use of jdk.internal.module.ModuleHashes.HashSupplier in project Bytecoder by mirkosertic.

the class ModuleReferences method newJarModule.

/**
 * Creates a ModuleReference to a possibly-patched module in a modular JAR.
 */
static ModuleReference newJarModule(ModuleInfo.Attributes attrs, ModulePatcher patcher, Path file) {
    URI uri = file.toUri();
    Supplier<ModuleReader> supplier = () -> new JarModuleReader(file, uri);
    HashSupplier hasher = (a) -> ModuleHashes.computeHash(file, a);
    return newModule(attrs, uri, supplier, patcher, 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 4 with HashSupplier

use of jdk.internal.module.ModuleHashes.HashSupplier 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 5 with HashSupplier

use of jdk.internal.module.ModuleHashes.HashSupplier in project Bytecoder by mirkosertic.

the class SystemModuleFinders method toModuleReference.

/**
 * Creates a ModuleReference to the system module.
 */
static ModuleReference toModuleReference(ModuleDescriptor descriptor, ModuleTarget target, ModuleHashes recordedHashes, HashSupplier hasher, ModuleResolution mres) {
    String mn = descriptor.name();
    URI uri = JNUA.create("jrt", "/".concat(mn));
    Supplier<ModuleReader> readerSupplier = new Supplier<>() {

        @Override
        public ModuleReader get() {
            return new SystemModuleReader(mn, uri);
        }
    };
    ModuleReference mref = new ModuleReferenceImpl(descriptor, uri, readerSupplier, null, target, recordedHashes, hasher, mres);
    // may need a reference to a patched module if --patch-module specified
    mref = ModuleBootstrap.patcher().patchIfNeeded(mref);
    return mref;
}
Also used : ModuleReader(java.lang.module.ModuleReader) ModuleReference(java.lang.module.ModuleReference) Supplier(java.util.function.Supplier) HashSupplier(jdk.internal.module.ModuleHashes.HashSupplier) URI(java.net.URI)

Aggregations

ModuleReference (java.lang.module.ModuleReference)5 HashSupplier (jdk.internal.module.ModuleHashes.HashSupplier)5 ModuleReader (java.lang.module.ModuleReader)3 URI (java.net.URI)3 Supplier (java.util.function.Supplier)3 File (java.io.File)2 IOError (java.io.IOError)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 ByteBuffer (java.nio.ByteBuffer)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Lock (java.util.concurrent.locks.Lock)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2