Search in sources :

Example 1 with ModuleReader

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

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

the class BuiltinClassLoader method defineClass.

/**
 * Defines the given binary class name to the VM, loading the class
 * bytes from the given module.
 *
 * @return the resulting Class or {@code null} if an I/O error occurs
 */
private Class<?> defineClass(String cn, LoadedModule loadedModule) {
    ModuleReference mref = loadedModule.mref();
    ModuleReader reader = moduleReaderFor(mref);
    try {
        ByteBuffer bb = null;
        URL csURL = null;
        // locate class file, special handling for patched modules to
        // avoid locating the resource twice
        String rn = cn.replace('.', '/').concat(".class");
        if (reader instanceof PatchedModuleReader) {
            Resource r = ((PatchedModuleReader) reader).findResource(rn);
            if (r != null) {
                bb = r.getByteBuffer();
                csURL = r.getCodeSourceURL();
            }
        } else {
            bb = reader.read(rn).orElse(null);
            csURL = loadedModule.codeSourceURL();
        }
        if (bb == null) {
            // class not found
            return null;
        }
        CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null);
        try {
            // define class to VM
            return defineClass(cn, bb, cs);
        } finally {
            reader.release(bb);
        }
    } catch (IOException ioe) {
        // TBD on how I/O errors should be propagated
        return null;
    }
}
Also used : PatchedModuleReader(jdk.internal.module.ModulePatcher.PatchedModuleReader) PatchedModuleReader(jdk.internal.module.ModulePatcher.PatchedModuleReader) ModuleReader(java.lang.module.ModuleReader) ModuleReference(java.lang.module.ModuleReference) IOException(java.io.IOException) CodeSource(java.security.CodeSource) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) CodeSigner(java.security.CodeSigner)

Example 3 with ModuleReader

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

the class BuiltinClassLoader method moduleReaderFor.

// -- miscellaneous supporting methods
/**
 * Returns the ModuleReader for the given module, creating it if needed
 */
private ModuleReader moduleReaderFor(ModuleReference mref) {
    ModuleReader reader = moduleToReader.get(mref);
    if (reader == null) {
        // avoid method reference during startup
        Function<ModuleReference, ModuleReader> create = new Function<>() {

            public ModuleReader apply(ModuleReference moduleReference) {
                try {
                    return mref.open();
                } catch (IOException e) {
                    // load attempting to open the module again.
                    return new NullModuleReader();
                }
            }
        };
        reader = moduleToReader.computeIfAbsent(mref, create);
    }
    return reader;
}
Also used : Function(java.util.function.Function) PatchedModuleReader(jdk.internal.module.ModulePatcher.PatchedModuleReader) ModuleReader(java.lang.module.ModuleReader) ModuleReference(java.lang.module.ModuleReference) IOException(java.io.IOException)

Example 4 with ModuleReader

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

the class Loader method defineClass.

/**
 * Defines the given binary class name to the VM, loading the class
 * bytes from the given module.
 *
 * @return the resulting Class or {@code null} if an I/O error occurs
 */
private Class<?> defineClass(String cn, LoadedModule loadedModule) {
    ModuleReader reader = moduleReaderFor(loadedModule.mref());
    try {
        // read class file
        String rn = cn.replace('.', '/').concat(".class");
        ByteBuffer bb = reader.read(rn).orElse(null);
        if (bb == null) {
            // class not found
            return null;
        }
        try {
            return defineClass(cn, bb, loadedModule.codeSource());
        } finally {
            reader.release(bb);
        }
    } catch (IOException ioe) {
        // TBD on how I/O errors should be propagated
        return null;
    }
}
Also used : ModuleReader(java.lang.module.ModuleReader) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ModuleReader

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

Aggregations

ModuleReader (java.lang.module.ModuleReader)6 IOException (java.io.IOException)5 ModuleReference (java.lang.module.ModuleReference)5 ByteBuffer (java.nio.ByteBuffer)4 UncheckedIOException (java.io.UncheckedIOException)3 URI (java.net.URI)3 Supplier (java.util.function.Supplier)3 HashSupplier (jdk.internal.module.ModuleHashes.HashSupplier)3 File (java.io.File)2 IOError (java.io.IOError)2 InputStream (java.io.InputStream)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 List (java.util.List)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 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 JarEntry (java.util.jar.JarEntry)2