Java Code Examples for java.lang.module.ModuleReader#release()

The following examples show how to use java.lang.module.ModuleReader#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Loader.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 2
Source File: Loader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 3
Source File: ModuleReaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test ModuleReader#read
 */
void testRead(ModuleReader reader, String name, byte[] expectedBytes)
    throws IOException
{
    Optional<ByteBuffer> obb = reader.read(name);
    assertTrue(obb.isPresent());

    ByteBuffer bb = obb.get();
    try {
        int rem = bb.remaining();
        assertTrue(rem == expectedBytes.length);
        byte[] bytes = new byte[rem];
        bb.get(bytes);
        assertTrue(Arrays.equals(bytes, expectedBytes));
    } finally {
        reader.release(bb);
    }
}
 
Example 4
Source File: BuiltinClassLoader.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }
}
 
Example 5
Source File: BuiltinClassLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
    }
}