Search in sources :

Example 56 with URLClassLoader

use of java.net.URLClassLoader in project weave by continuuity.

the class ApplicationBundlerTest method createClassLoader.

private ClassLoader createClassLoader(File dir) throws MalformedURLException {
    List<URL> urls = Lists.newArrayList();
    urls.add(new File(dir, "classes").toURI().toURL());
    File[] libFiles = new File(dir, "lib").listFiles();
    if (libFiles != null) {
        for (File file : libFiles) {
            urls.add(file.toURI().toURL());
        }
    }
    return new URLClassLoader(urls.toArray(new URL[0])) {

        @Override
        protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            // Load class from the given URLs first before delegating to parent.
            try {
                return super.findClass(name);
            } catch (ClassNotFoundException e) {
                ClassLoader parent = getParent();
                return parent == null ? ClassLoader.getSystemClassLoader().loadClass(name) : parent.loadClass(name);
            }
        }
    };
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL)

Example 57 with URLClassLoader

use of java.net.URLClassLoader in project cucumber-jvm by cucumber.

the class UtilsTest method testInvokeOtherClassLoader.

@Test
public void testInvokeOtherClassLoader() throws Throwable {
    // Create a new class loader using the same URLs as the system class loader
    URL[] urls = ((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs();
    ClassLoader myClassLoader = new URLClassLoader(urls, null);
    // Load the test class MyClass using our newly create class loader and create an instance.
    Class<?> myClass = myClassLoader.loadClass(MyClass.class.getName());
    Object target = myClass.getConstructor().newInstance();
    // Sanity check to verify that MyClass was loaded by the default class loader.
    assertEquals(ClassLoader.getSystemClassLoader(), MyClass.class.getClassLoader());
    // Verify that the class loader of our loaded class is different.
    assertNotEquals(MyClass.class.getClassLoader(), myClass.getClassLoader());
    Method publicInterfaceMethod = MyClass.class.getMethod("publicInterfaceMethod");
    assertEquals(Boolean.TRUE, Utils.invoke(target, publicInterfaceMethod, 1000));
    Method protectedAbstractMethod = MyClass.class.getDeclaredMethod("protectedAbstractMethod");
    assertEquals(Boolean.TRUE, Utils.invoke(target, protectedAbstractMethod, 1000));
    Method privateMethod = MyAbstractClass.class.getDeclaredMethod("privateMethod");
    assertEquals(Boolean.TRUE, Utils.invoke(target, privateMethod, 1000));
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method) URL(java.net.URL) Test(org.junit.Test)

Example 58 with URLClassLoader

use of java.net.URLClassLoader in project hadoop by apache.

the class TestReflectionUtils method testCacheDoesntLeak.

@SuppressWarnings("unchecked")
@Test
public void testCacheDoesntLeak() throws Exception {
    // very fast, but a bit less reliable - bigger numbers force GC
    int iterations = 9999;
    for (int i = 0; i < iterations; i++) {
        URLClassLoader loader = new URLClassLoader(new URL[0], getClass().getClassLoader());
        Class cl = Class.forName("org.apache.hadoop.util.TestReflectionUtils$LoadedInChild", false, loader);
        Object o = ReflectionUtils.newInstance(cl, null);
        assertEquals(cl, o.getClass());
    }
    System.gc();
    assertTrue(cacheSize() + " too big", cacheSize() < iterations);
}
Also used : URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 59 with URLClassLoader

use of java.net.URLClassLoader in project spark by perwendel.

the class StaticFilesFromArchiveTest method createExtendedClassLoader.

private static URLClassLoader createExtendedClassLoader() {
    URL[] parentURLs = ((URLClassLoader) getSystemClassLoader()).getURLs();
    URL[] urls = new URL[parentURLs.length + 1];
    arraycopy(parentURLs, 0, urls, 0, parentURLs.length);
    URL publicJar = StaticFilesFromArchiveTest.class.getResource("/public-jar.zip");
    urls[urls.length - 1] = publicJar;
    // no parent classLoader because Spark and the static resources need to be loaded from the same classloader
    return new URLClassLoader(urls, null);
}
Also used : URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 60 with URLClassLoader

use of java.net.URLClassLoader in project jodd by oblac.

the class ClassLoaderUtil method getDefaultClasspath.

/**
	 * Returns default class path from all available <code>URLClassLoader</code>
	 * in classloader hierarchy. The following is added to the classpath list:
	 * <ul>
	 * <li>file URLs from <code>URLClassLoader</code> (other URL protocols are ignored)</li>
	 * <li>inner entries from containing <b>manifest</b> files (if exist)</li>
	 * <li>bootstrap classpath</li>
	 * </ul>
	 */
public static File[] getDefaultClasspath(ClassLoader classLoader) {
    Set<File> classpaths = new TreeSet<>();
    while (classLoader != null) {
        if (classLoader instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) classLoader).getURLs();
            for (URL u : urls) {
                File f = FileUtil.toFile(u);
                if ((f != null) && f.exists()) {
                    try {
                        f = f.getCanonicalFile();
                        boolean newElement = classpaths.add(f);
                        if (newElement) {
                            addInnerClasspathItems(classpaths, f);
                        }
                    } catch (IOException ignore) {
                    }
                }
            }
        }
        classLoader = classLoader.getParent();
    }
    String bootstrap = SystemUtil.getSunBootClassPath();
    if (bootstrap != null) {
        String[] bootstrapFiles = StringUtil.splitc(bootstrap, File.pathSeparatorChar);
        for (String bootstrapFile : bootstrapFiles) {
            File f = new File(bootstrapFile);
            if (f.exists()) {
                try {
                    f = f.getCanonicalFile();
                    boolean newElement = classpaths.add(f);
                    if (newElement) {
                        addInnerClasspathItems(classpaths, f);
                    }
                } catch (IOException ignore) {
                }
            }
        }
    }
    File[] result = new File[classpaths.size()];
    return classpaths.toArray(result);
}
Also used : TreeSet(java.util.TreeSet) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

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