Search in sources :

Example 1 with ImageLocation

use of jdk.internal.jimage.ImageLocation 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 ImageLocation

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

the class JavaRuntimeURLConnection method findResource.

/**
 * Finds a resource in a module, returning {@code null} if the resource
 * is not found.
 */
private static Resource findResource(String module, String name) {
    if (reader != null) {
        URL url = toJrtURL(module, name);
        ImageLocation location = reader.findLocation(module, name);
        if (location != null && URLClassPath.checkURL(url) != null) {
            return new Resource() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public URL getURL() {
                    return url;
                }

                @Override
                public URL getCodeSourceURL() {
                    return toJrtURL(module);
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    byte[] resource = reader.getResource(location);
                    return new ByteArrayInputStream(resource);
                }

                @Override
                public int getContentLength() {
                    long size = location.getUncompressedSize();
                    return (size > Integer.MAX_VALUE) ? -1 : (int) size;
                }
            };
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(jdk.internal.loader.Resource) URL(java.net.URL) ImageLocation(jdk.internal.jimage.ImageLocation)

Aggregations

ImageLocation (jdk.internal.jimage.ImageLocation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ModuleReference (java.lang.module.ModuleReference)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ImageReader (jdk.internal.jimage.ImageReader)1 Resource (jdk.internal.loader.Resource)1 HashSupplier (jdk.internal.module.ModuleHashes.HashSupplier)1