use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.
the class LeakTest method testShadow.
private static WeakReference<ClassLoader> testShadow(Class<?> originalTestClass) throws Exception {
URLClassLoader originalLoader = (URLClassLoader) originalTestClass.getClassLoader();
URL[] urls = originalLoader.getURLs();
URLClassLoader shadowLoader = new ShadowClassLoader(urls, originalLoader.getParent());
System.out.println("Shadow loader is " + shadowLoader);
String className = originalTestClass.getName();
Class<?> testClass = Class.forName(className, false, shadowLoader);
if (testClass.getClassLoader() != shadowLoader) {
throw new IllegalArgumentException("Loader didn't work: " + testClass.getClassLoader() + " != " + shadowLoader);
}
Method main = testClass.getMethod("main", String[].class);
main.invoke(null, (Object) new String[0]);
return new WeakReference<ClassLoader>(shadowLoader);
}
use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.
the class Test7064279 method test.
private static Object test(String jarName, String className) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append(jarName);
ClassLoader newLoader = new URLClassLoader(new URL[] { new URL(sb.toString()) });
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(newLoader);
test(newLoader.loadClass(className));
Thread.currentThread().setContextClassLoader(oldLoader);
return newLoader;
}
use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.
the class TwiceIndirectlyLoadABundle method loadAndTest.
public boolean loadAndTest() throws Throwable {
// Find out where we are running from so we can setup the URLClassLoader URLs
// test.src and test.classes will be set if running in jtreg, but probably
// not otherwise
String testDir = System.getProperty("test.src", System.getProperty("user.dir"));
String testClassesDir = System.getProperty("test.classes", System.getProperty("user.dir"));
URL[] urls = new URL[2];
// Allow for both jtreg and standalone cases here
// Unlike the 1-level test where we can get the bundle from the caller's
// class loader, for this one we don't want to expose the resource directory
// to the next class. That way we're invoking the LoadItUp2Invoker class
// from this class that does have access to the resources (two levels
// up the call stack), but the Invoker itself won't have access to resource
urls[0] = Paths.get(testDir, "resources").toUri().toURL();
urls[1] = Paths.get(testClassesDir).toUri().toURL();
// Make sure we can find it via the URLClassLoader
URLClassLoader yetAnotherResourceCL = new URLClassLoader(urls, null);
Class<?> loadItUp2InvokerClazz = Class.forName("LoadItUp2Invoker", true, yetAnotherResourceCL);
ClassLoader actual = loadItUp2InvokerClazz.getClassLoader();
if (actual != yetAnotherResourceCL) {
throw new Exception("LoadItUp2Invoker was loaded by an unexpected CL: " + actual);
}
Object loadItUp2Invoker = loadItUp2InvokerClazz.newInstance();
Method setupMethod = loadItUp2InvokerClazz.getMethod("setup", urls.getClass(), String.class);
try {
// For the next class loader we create, we want to leave off
// the resources. That way loadItUp2Invoker will have access to
// them, but the next class won't.
URL[] noResourceUrl = new URL[1];
// from above -- just the test classes
noResourceUrl[0] = urls[1];
setupMethod.invoke(loadItUp2Invoker, noResourceUrl, rbName);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
Method testMethod = loadItUp2InvokerClazz.getMethod("test");
try {
return (Boolean) testMethod.invoke(loadItUp2Invoker);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.
the class ResourceBundleSearchTest method runTests.
private void runTests() throws Throwable {
// ensure we are using en as the default Locale so we can find the resource
Locale.setDefault(Locale.ENGLISH);
ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
// Find out where we are running from so we can setup the URLClassLoader URL
String userDir = System.getProperty("user.dir");
String testDir = System.getProperty("test.src", userDir);
URL[] urls = new URL[1];
urls[0] = Paths.get(testDir, "resources").toUri().toURL();
URLClassLoader rbClassLoader = new URLClassLoader(urls);
int testnb = 1;
System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
// Test 1 - can we find a Logger bundle from doing a stack search?
// We shouldn't be able to
assertFalse(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch");
System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
// Test 2 - can we find a Logger bundle off of the Thread context class
// loader? We should be able to.
assertTrue(testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader), "2-testGetBundleFromTCCL");
System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
// can see it there.
if (isOnClassPath(PROP_RB_NAME, myClassLoader)) {
debug("We should be able to see " + PROP_RB_NAME + " on the classpath");
assertTrue(testGetBundleFromSystemClassLoader(PROP_RB_NAME), "3-testGetBundleFromSystemClassLoader");
} else {
throw new Exception("TEST SETUP FAILURE: Cannot see " + PROP_RB_NAME + " on the classpath");
}
System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
// Test 4 - we should be able to find a bundle from the caller's
// classloader, but only one level up.
assertTrue(testGetBundleFromCallersClassLoader(), "4-testGetBundleFromCallersClassLoader");
System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
// Test 5 - this ensures that getAnonymousLogger(String rbName)
// can find the bundle from the caller's classloader
assertTrue(testGetAnonymousLogger(), "5-testGetAnonymousLogger");
System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
// Test 6 - first call getLogger("myLogger").
// Then call getLogger("myLogger","bundleName") from a different ClassLoader
// Make sure we find the bundle
assertTrue(testGetBundleFromSecondCallersClassLoader(), "6-testGetBundleFromSecondCallersClassLoader");
System.out.println("ResourceBundleSearchTest generating report: " + getTimeStamp());
report();
}
use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.
the class IndirectlyLoadABundle method getLoggerWithNewCL.
private boolean getLoggerWithNewCL(URL[] urls, String loggerName, String bundleName) throws Throwable {
Logger result = null;
;
// Test getLogger("foo"); getLogger("foo", "rbName");
// First do the getLogger() call with no bundle name
URLClassLoader getLoggerCL = new URLClassLoader(urls, null);
Class<?> loadItUpClazz1 = Class.forName("LoadItUp1", true, getLoggerCL);
ClassLoader actual = loadItUpClazz1.getClassLoader();
if (actual != getLoggerCL) {
throw new Exception("LoadItUp1 was loaded by an unexpected CL: " + actual);
}
Object loadItUp1 = loadItUpClazz1.newInstance();
if (bundleName != null) {
Method getLoggerMethod = loadItUpClazz1.getMethod("getLogger", String.class, String.class);
try {
result = (Logger) getLoggerMethod.invoke(loadItUp1, loggerName, bundleName);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
} else {
Method getLoggerMethod = loadItUpClazz1.getMethod("getLogger", String.class);
try {
result = (Logger) getLoggerMethod.invoke(loadItUp1, loggerName);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
return result != null;
}
Aggregations