Search in sources :

Example 1 with PatchedModuleReader

use of jdk.internal.module.ModulePatcher.PatchedModuleReader 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)

Aggregations

IOException (java.io.IOException)1 ModuleReader (java.lang.module.ModuleReader)1 ModuleReference (java.lang.module.ModuleReference)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 CodeSigner (java.security.CodeSigner)1 CodeSource (java.security.CodeSource)1 PatchedModuleReader (jdk.internal.module.ModulePatcher.PatchedModuleReader)1