Search in sources :

Example 1 with Resource

use of jdk.internal.loader.Resource in project Bytecoder by mirkosertic.

the class FactoryURLClassLoader method findClass.

/**
 * Finds and loads the class with the specified name from the URL search
 * path. Any URLs referring to JAR files are loaded and opened as needed
 * until the class is found.
 *
 * @param name the name of the class
 * @return the resulting class
 * @exception ClassNotFoundException if the class could not be found,
 *            or if the loader is closed.
 * @exception NullPointerException if {@code name} is {@code null}.
 */
protected Class<?> findClass(final String name) throws ClassNotFoundException {
    final Class<?> result;
    try {
        result = AccessController.doPrivileged(new PrivilegedExceptionAction<>() {

            public Class<?> run() throws ClassNotFoundException {
                String path = name.replace('.', '/').concat(".class");
                Resource res = ucp.getResource(path, false);
                if (res != null) {
                    try {
                        return defineClass(name, res);
                    } catch (IOException e) {
                        throw new ClassNotFoundException(name, e);
                    }
                } else {
                    return null;
                }
            }
        }, acc);
    } catch (java.security.PrivilegedActionException pae) {
        throw (ClassNotFoundException) pae.getException();
    }
    if (result == null) {
        throw new ClassNotFoundException(name);
    }
    return result;
}
Also used : Resource(jdk.internal.loader.Resource) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException)

Example 2 with Resource

use of jdk.internal.loader.Resource 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

Resource (jdk.internal.loader.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 ImageLocation (jdk.internal.jimage.ImageLocation)1