Search in sources :

Example 1 with ImageReader

use of jdk.internal.jimage.ImageReader 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 2 with ImageReader

use of jdk.internal.jimage.ImageReader in project Bytecoder by mirkosertic.

the class SystemImage method open.

static SystemImage open() throws IOException {
    if (modulesImageExists) {
        // open a .jimage and build directory structure
        final ImageReader image = ImageReader.open(moduleImageFile);
        image.getRootDirectory();
        return new SystemImage() {

            @Override
            Node findNode(String path) throws IOException {
                return image.findNode(path);
            }

            @Override
            byte[] getResource(Node node) throws IOException {
                return image.getResource(node);
            }

            @Override
            void close() throws IOException {
                image.close();
            }
        };
    }
    if (Files.notExists(explodedModulesDir))
        throw new FileSystemNotFoundException(explodedModulesDir.toString());
    return new ExplodedImage(explodedModulesDir);
}
Also used : FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) Node(jdk.internal.jimage.ImageReader.Node) ImageReader(jdk.internal.jimage.ImageReader)

Aggregations

ImageReader (jdk.internal.jimage.ImageReader)2 ModuleReference (java.lang.module.ModuleReference)1 FileSystemNotFoundException (java.nio.file.FileSystemNotFoundException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ImageLocation (jdk.internal.jimage.ImageLocation)1 Node (jdk.internal.jimage.ImageReader.Node)1 HashSupplier (jdk.internal.module.ModuleHashes.HashSupplier)1