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);
}
}
};
}
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));
}
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);
}
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);
}
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);
}
Aggregations