Search in sources :

Example 71 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class MetadataFormatThreadTest method createTest.

protected static Thread createTest(String codebase, String code) throws Exception {
    URL[] urls = { new File(codebase).toURL() };
    final ClassLoader loader = new URLClassLoader(urls);
    final Thread t = new Thread(new MetadataFormatThreadTest(code));
    java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            t.setContextClassLoader(loader);
            return null;
        }
    });
    return t;
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL)

Example 72 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class ClassLoad method main.

public static void main(String[] args) throws Exception {
    boolean error = true;
    // Start a dummy server to return 404
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    HttpHandler handler = new HttpHandler() {

        public void handle(HttpExchange t) throws IOException {
            InputStream is = t.getRequestBody();
            while (is.read() != -1) ;
            t.sendResponseHeaders(404, -1);
            t.close();
        }
    };
    server.createContext("/", handler);
    server.start();
    // Client request
    try {
        URL url = new URL("http://localhost:" + server.getAddress().getPort());
        String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
        ClassLoader loader = new URLClassLoader(new URL[] { url });
        System.out.println(url);
        Class c = loader.loadClass(name);
        System.out.println("Loaded class \"" + c.getName() + "\".");
    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
        error = false;
    } finally {
        server.stop(0);
    }
    if (error)
        throw new RuntimeException("No ClassNotFoundException generated");
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) HttpServer(com.sun.net.httpserver.HttpServer) URLClassLoader(java.net.URLClassLoader)

Example 73 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class LoadProxyClasses method main.

public static void main(String[] args) {
    try {
        System.err.println("\nFunctional test to verify that RMI " + "loads proxy classes correctly\n");
        /* install proxy interfaces */
        publicUrl = TestLibrary.installClassInCodebase("PublicInterface", "public");
        URL publicUrl1 = TestLibrary.installClassInCodebase("PublicInterface1", "public1");
        URL nonpublicUrl = TestLibrary.installClassInCodebase("NonpublicInterface", "nonpublic", false);
        URL nonpublicUrl1 = TestLibrary.installClassInCodebase("NonpublicInterface1", "nonpublic1", false);
        URL bothNonpublicUrl = TestLibrary.installClassInCodebase("NonpublicInterface", "bothNonpublic");
        TestLibrary.installClassInCodebase("NonpublicInterface1", "bothNonpublic");
        URL fnnUrl = TestLibrary.installClassInCodebase("FnnClass", "fnn");
        TestLibrary.suggestSecurityManager(null);
        /* Case 1 */
        ClassLoader grandParentPublic = new URLClassLoader(new URL[] { publicUrl });
        ClassLoader parentNonpublic = new URLClassLoader(new URL[] { nonpublicUrl }, grandParentPublic);
        URLClassLoader fnnLoader1 = new URLClassLoader(new URL[] { fnnUrl }, parentNonpublic);
        Class nonpublicInterface = fnnLoader1.loadClass("NonpublicInterface");
        Class publicInterface = fnnLoader1.loadClass("PublicInterface");
        Proxy proxy1 = (Proxy) Proxy.newProxyInstance(parentNonpublic, new Class[] { nonpublicInterface, publicInterface }, new TestInvocationHandler());
        unmarshalProxyClass(proxy1, fnnLoader1, parentNonpublic, 1, null);
        /* Case 2 */
        Class zipConstantsClass = Class.forName("java.util.zip.ZipConstants");
        URLClassLoader fnnLoader2 = new URLClassLoader(new URL[] { fnnUrl });
        Proxy proxy2 = (Proxy) Proxy.newProxyInstance(null, new Class[] { zipConstantsClass, Checksum.class }, new TestInvocationHandler());
        unmarshalProxyClass(proxy2, fnnLoader2, (ClassLoader) null, 2, null);
        /* Case 3 */
        Thread currentThread = Thread.currentThread();
        ClassLoader fnnLoader3 = new URLClassLoader(new URL[] { publicUrl, fnnUrl });
        ClassLoader newCtxLoader = new URLClassLoader(new URL[] { publicUrl }, fnnLoader3);
        Class publicInterface3 = fnnLoader3.loadClass("PublicInterface");
        ClassLoader currentCtxLoader = currentThread.getContextClassLoader();
        currentThread.setContextClassLoader(newCtxLoader);
        Proxy proxy3 = (Proxy) Proxy.newProxyInstance(newCtxLoader, new Class[] { publicInterface3 }, new TestInvocationHandler());
        unmarshalProxyClass(proxy3, fnnLoader3, fnnLoader3, 3, new Case3Checker());
        currentThread.setContextClassLoader(currentCtxLoader);
        /* Case 4 */
        ClassLoader bothNonpublicLoader = new URLClassLoader(new URL[] { bothNonpublicUrl });
        Class nonpublicInterface4a = bothNonpublicLoader.loadClass("NonpublicInterface");
        Class nonpublicInterface4b = bothNonpublicLoader.loadClass("NonpublicInterface1");
        Proxy proxy4 = (Proxy) Proxy.newProxyInstance(bothNonpublicLoader, new Class[] { nonpublicInterface4a, nonpublicInterface4b }, new TestInvocationHandler());
        ClassLoader nonpublicLoaderA = new URLClassLoader(new URL[] { nonpublicUrl });
        ClassLoader nonpublicLoaderB = new URLClassLoader(new URL[] { nonpublicUrl1 }, nonpublicLoaderA);
        currentCtxLoader = currentThread.getContextClassLoader();
        currentThread.setContextClassLoader(nonpublicLoaderB);
        IllegalAccessError illegal = null;
        try {
            unmarshalProxyClass(proxy4, fnnLoader2, nonpublicLoaderB, 4, null);
        } catch (IllegalAccessError e) {
            illegal = e;
        }
        if (illegal == null) {
            TestLibrary.bomb("case4: IllegalAccessError not thrown " + "when multiple nonpublic interfaces have \n" + "different class loaders");
        } else {
            System.err.println("\ncase4: IllegalAccessError correctly " + "thrown \n when trying to load proxy " + "with multiple nonpublic interfaces in \n" + "  different class loaders");
        }
        currentThread.setContextClassLoader(currentCtxLoader);
        /* Case 5*/
        ClassLoader publicLoader = new URLClassLoader(new URL[] { publicUrl });
        Class publicInterface5 = publicLoader.loadClass("PublicInterface");
        Proxy proxy5 = (Proxy) Proxy.newProxyInstance(publicLoader, new Class[] { publicInterface5 }, new TestInvocationHandler());
        currentCtxLoader = currentThread.getContextClassLoader();
        currentThread.setContextClassLoader(publicLoader);
        unmarshalProxyClass(proxy5, fnnLoader2, publicLoader, 5, new Case5Checker());
        currentThread.setContextClassLoader(currentCtxLoader);
        /* Case 6 */
        ClassLoader fnnLoader6 = new URLClassLoader(new URL[] { fnnUrl, publicUrl });
        ClassLoader publicLoader6 = new URLClassLoader(new URL[] { publicUrl1 }, fnnLoader6);
        Class publicInterface6a = publicLoader6.loadClass("PublicInterface1");
        Class publicInterface6b = fnnLoader6.loadClass("PublicInterface");
        Proxy proxy6 = (Proxy) Proxy.newProxyInstance(publicLoader6, new Class[] { publicInterface6a, publicInterface6b }, new TestInvocationHandler());
        ClassNotFoundException cnfe = null;
        try {
            unmarshalProxyClass(proxy6, fnnLoader6, publicLoader6, 6, null);
        } catch (ClassNotFoundException e) {
            cnfe = e;
        }
        if (cnfe == null) {
            TestLibrary.bomb("ClassNotFoundException not thrown " + "when not all proxy interfaces could " + " be found in a single class loader ");
        } else {
            System.err.println("Case6: ClassNotFoundException " + "correctly thrown when not all proxy" + " interfaces could be found in a " + "single class loader");
            cnfe.printStackTrace();
        }
        System.err.println("TEST PASSED");
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        TestLibrary.bomb(e);
    }
}
Also used : URL(java.net.URL) IOException(java.io.IOException) Proxy(java.lang.reflect.Proxy) Checksum(java.util.zip.Checksum) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) RMIClassLoader(java.rmi.server.RMIClassLoader)

Example 74 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class DefaultMethodRegressionTestsRun method runClass.

static void runClass(File classPath, String classname) throws Exception {
    URL[] urls = { classPath.toURI().toURL() };
    ClassLoader loader = new URLClassLoader(urls);
    Class<?> c = loader.loadClass(classname);
    Class<?>[] argTypes = new Class<?>[] { String[].class };
    Object[] methodArgs = new Object[] { null };
    Method method = c.getMethod("main", argTypes);
    method.invoke(c, methodArgs);
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method) URL(java.net.URL)

Example 75 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class ClassRestrictions method main.

public static void main(String[] args) {
    System.err.println("\nTest of restrictions on parameters to Proxy.getProxyClass\n");
    try {
        ClassLoader loader = ClassRestrictions.class.getClassLoader();
        Class<?>[] interfaces;
        Class<?> proxyClass;
        /*
             * All of the Class objects in the interfaces array must represent
             * interfaces, not classes or primitive types.
             */
        try {
            interfaces = new Class<?>[] { Object.class };
            proxyClass = Proxy.getProxyClass(loader, interfaces);
            throw new Error("proxy class created with java.lang.Object as interface");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println();
        // assume exception is for intended failure
        }
        try {
            interfaces = new Class<?>[] { Integer.TYPE };
            proxyClass = Proxy.getProxyClass(loader, interfaces);
            throw new Error("proxy class created with int.class as interface");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println();
        // assume exception is for intended failure
        }
        /*
             * No two elements in the interfaces array may refer to identical
             * Class objects.
             */
        try {
            interfaces = new Class<?>[] { Bar.class, Bar.class };
            proxyClass = Proxy.getProxyClass(loader, interfaces);
            throw new Error("proxy class created with repeated interfaces");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println();
        // assume exception is for intended failure
        }
        /*
             * All of the interfaces types must be visible by name though the
             * specified class loader.
             */
        ClassLoader altLoader = new URLClassLoader(((URLClassLoader) loader).getURLs(), null);
        Class altBarClass;
        altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
        try {
            interfaces = new Class<?>[] { altBarClass };
            proxyClass = Proxy.getProxyClass(loader, interfaces);
            throw new Error("proxy class created with interface " + "not visible to class loader");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println();
        // assume exception is for intended failure
        }
        /*
             * All non-public interfaces must be in the same package.
             */
        Class<?> nonPublic1 = Bashful.class;
        Class<?> nonPublic2 = Class.forName(nonPublicIntrfaceName);
        if (Modifier.isPublic(nonPublic2.getModifiers())) {
            throw new Error("Interface " + nonPublicIntrfaceName + " is public and need to be changed!");
        }
        try {
            interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
            proxyClass = Proxy.getProxyClass(loader, interfaces);
            throw new Error("proxy class created with two non-public interfaces " + "in different packages");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println();
        // assume exception is for intended failure
        }
        /*
             * No two interfaces may each have a method with the same name and
             * parameter signature but different return type.
             */
        try {
            interfaces = new Class<?>[] { Bar.class, Baz.class };
            proxyClass = Proxy.getProxyClass(loader, interfaces);
            throw new Error("proxy class created with conflicting methods");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.err.println();
        // assume exception is for intended failure
        }
        /*
             * All components of this test have passed.
             */
        System.err.println("\nTEST PASSED");
    } catch (Throwable e) {
        System.err.println("\nTEST FAILED:");
        e.printStackTrace();
        throw new Error("TEST FAILED: ", e);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27