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);
}
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;
}
}
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;
}
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;
}
}
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);
}
Aggregations