Java Code Examples for java.net.URLClassLoader#getParent()

The following examples show how to use java.net.URLClassLoader#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LeakTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: LeakTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: LeakTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: LeakTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: Test6823354.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void loadandrunclass(String classname) throws Exception {
    Class cl = Class.forName(classname);
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
    ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
    Class c = loader.loadClass(classname);
    Runnable r = (Runnable) c.newInstance();
    r.run();
}
 
Example 6
Source File: StandaloneApp.java    From james with Apache License 2.0 5 votes vote down vote up
private static void printClassLoaderURLs(URLClassLoader urlClassLoader) {
    System.out.println();
    outs(urlClassLoader);
    Stream.of(urlClassLoader.getURLs()).forEach(url -> outs("..", url));

    if (urlClassLoader.getParent() != null) {
        printClassLoaderURLs((URLClassLoader) urlClassLoader.getParent());
    }
}
 
Example 7
Source File: Test6823354.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void loadandrunclass(String classname) throws Exception {
    Class cl = Class.forName(classname);
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
    ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
    Class c = loader.loadClass(classname);
    Runnable r = (Runnable) c.newInstance();
    r.run();
}
 
Example 8
Source File: Test6805724.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    Class cl = Class.forName("Test6805724");
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();

    // Iterate over all 2^k-1 divisors.
    for (int k = 1; k < Long.SIZE; k++) {
        long divisor = (1L << k) - 1;
        System.setProperty("divisor", "" + divisor);
        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
        Class c = loader.loadClass("Test6805724");
        Runnable r = (Runnable) c.newInstance();
        r.run();
    }
}
 
Example 9
Source File: Test6800154.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
    Class cl = Class.forName("Test6800154");
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();

    // Iterate over all divisors.
    for (int i = 0; i < DIVISORS.length; i++) {
        System.setProperty("divisor", "" + DIVISORS[i]);
        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
        Class c = loader.loadClass("Test6800154");
        Runnable r = (Runnable) c.newInstance();
        r.run();
    }
}
 
Example 10
Source File: Test6805724.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    Class cl = Class.forName("Test6805724");
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();

    // Iterate over all 2^k-1 divisors.
    for (int k = 1; k < Long.SIZE; k++) {
        long divisor = (1L << k) - 1;
        System.setProperty("divisor", "" + divisor);
        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
        Class c = loader.loadClass("Test6805724");
        Runnable r = (Runnable) c.newInstance();
        r.run();
    }
}
 
Example 11
Source File: VelocityClassLoaderTest.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
/**
 * Make a different ClassLoader that loads the same URLs as this one, and use it to compile
 * an {@code @RetroFacebook} class. If Velocity loads its managers using the context class loader,
 * and that loader is still the original one that loaded this test, then it will find the
 * original copy of the Velocity classes rather than the one from the new loader, and fail.
 *
 * <p>This test assumes that the test class was loaded by a URLClassLoader and that that loader's
 * URLs also include the Velocity classes.
 */
public void testClassLoaderHack() throws Exception {
  URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  URLClassLoader newLoader = new URLClassLoader(myLoader.getURLs(), myLoader.getParent());
  String velocityClassName = Velocity.class.getName();
  Class<?> myVelocity = myLoader.loadClass(velocityClassName);
  Class<?> newVelocity = newLoader.loadClass(velocityClassName);
  assertThat(myVelocity).isNotEqualTo(newVelocity);
  Runnable test = (Runnable) newLoader.loadClass(RunInClassLoader.class.getName()).newInstance();
  assertThat(test.getClass()).isNotEqualTo(RunInClassLoader.class);
  test.run();
}
 
Example 12
Source File: Test6800154.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
    Class cl = Class.forName("Test6800154");
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();

    // Iterate over all divisors.
    for (int i = 0; i < DIVISORS.length; i++) {
        System.setProperty("divisor", "" + DIVISORS[i]);
        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
        Class c = loader.loadClass("Test6800154");
        Runnable r = (Runnable) c.newInstance();
        r.run();
    }
}
 
Example 13
Source File: ClassLoaderMap.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
/**
 * This method is modified to support value lookups where the key is a "proxy"
 * class loader representing the bootstrap class loader. This pattern is used
 * by ByteBuddy, whereby the proxy class loader is an {@code URLClassLoader}
 * that has an empty classpath and a null parent.
 * <p>
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unlikely-arg-type")
public T get(final Object key) {
  T value = super.get(key == null ? NULL : key);
  if (value != null || !(key instanceof URLClassLoader))
    return value;

  final URLClassLoader classLoader = (URLClassLoader)key;
  return classLoader.getURLs().length > 0 || classLoader.getParent() != null ? null : super.get(NULL);
}
 
Example 14
Source File: Test5057225.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void loadAndRunClass(String classname) throws Exception {
    Class cl = Class.forName(classname);
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
    ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
    Class c = loader.loadClass(classname);
    Runnable r = (Runnable) c.newInstance();
    r.run();
}
 
Example 15
Source File: VelocityClassLoaderTest.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
/**
 * Make a different ClassLoader that loads the same URLs as this one, and use it to compile
 * an {@code @RetroWeibo} class. If Velocity loads its managers using the context class loader,
 * and that loader is still the original one that loaded this test, then it will find the
 * original copy of the Velocity classes rather than the one from the new loader, and fail.
 *
 * <p>This test assumes that the test class was loaded by a URLClassLoader and that that loader's
 * URLs also include the Velocity classes.
 */
public void testClassLoaderHack() throws Exception {
  URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  URLClassLoader newLoader = new URLClassLoader(myLoader.getURLs(), myLoader.getParent());
  String velocityClassName = Velocity.class.getName();
  Class<?> myVelocity = myLoader.loadClass(velocityClassName);
  Class<?> newVelocity = newLoader.loadClass(velocityClassName);
  assertThat(myVelocity).isNotEqualTo(newVelocity);
  Runnable test = (Runnable) newLoader.loadClass(RunInClassLoader.class.getName()).newInstance();
  assertThat(test.getClass()).isNotEqualTo(RunInClassLoader.class);
  test.run();
}
 
Example 16
Source File: Test6800154.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
    Class cl = Class.forName("Test6800154");
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();

    // Iterate over all divisors.
    for (int i = 0; i < DIVISORS.length; i++) {
        System.setProperty("divisor", "" + DIVISORS[i]);
        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
        Class c = loader.loadClass("Test6800154");
        Runnable r = (Runnable) c.newInstance();
        r.run();
    }
}
 
Example 17
Source File: BeamIOTransformer.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined,
        final ProtectionDomain protectionDomain, final byte[] classfileBuffer) {
    if (className == null || !ConfigurableClassLoader.class.isInstance(loader)) {
        return classfileBuffer;
    }

    final ConfigurableClassLoader classLoader = ConfigurableClassLoader.class.cast(loader);
    final String javaClassName = toClassName(className);
    if (!KnownClassesFilter.INSTANCE.accept(javaClassName) && !canBeABeamIO(classLoader, javaClassName)) {
        return classfileBuffer;
    }

    final URLClassLoader tmpLoader = classLoader.createTemporaryCopy(); // cache it: mem is the issue?
    final Thread thread = Thread.currentThread();
    final ClassLoader old = thread.getContextClassLoader();
    thread.setContextClassLoader(tmpLoader);
    try {
        final Class<?> tmpClass = loadTempClass(tmpLoader, javaClassName);
        if (tmpClass.getClassLoader() != tmpLoader.getParent() && doesHierarchyContain(tmpClass, typesToEnhance)) {
            return rewrite(classLoader, javaClassName, classfileBuffer, tmpLoader, tmpClass);
        }
    } catch (final NoClassDefFoundError | ClassNotFoundException e) {
        if (DEBUG) {
            log.error("Can't load: " + className, e);
        }
    } finally {
        thread.setContextClassLoader(old);
    }
    return classfileBuffer;
}
 
Example 18
Source File: PluginClassLoader.java    From cherry with Apache License 2.0 5 votes vote down vote up
private URLClassLoader replaceClassLoader(final URLClassLoader oldLoader,
		final File base, final FileFilter filter) {

	if (null != base && base.canRead() && base.isDirectory()) {
		File[] files = base.listFiles(filter);

		if (null == files || 0 == files.length){
			logger.error("replaceClassLoader base dir:{} is empty", base.getAbsolutePath());
			return oldLoader;
		}

		logger.error("replaceClassLoader base dir: {} ,size: {}", base.getAbsolutePath(), files.length);
		
		URL[] oldElements = oldLoader.getURLs();
		URL[] elements = new URL[oldElements.length + files.length];
		System.arraycopy(oldElements, 0, elements, 0, oldElements.length);

		for (int j = 0; j < files.length; j++) {
			try {
				URL element = files[j].toURI().normalize().toURL();
				elements[oldElements.length + j] = element;
				
				logger.info("Adding '{}' to classloader", element.toString());
				
			} catch (MalformedURLException e) {
				logger.error("load jar file error", e);
			}
		}
		ClassLoader oldParent = oldLoader.getParent();
		IoUtils.closeQuietly(oldLoader); // best effort
		return URLClassLoader.newInstance(elements, oldParent);
	}
	
	return oldLoader;
}
 
Example 19
Source File: Test6823354.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void loadandrunclass(String classname) throws Exception {
    Class<?> cl = Class.forName(classname);
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
    ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
    Class<?> c = loader.loadClass(classname);
    Runnable r = (Runnable) c.newInstance();
    r.run();
}
 
Example 20
Source File: Test6800154.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
    Class cl = Class.forName("Test6800154");
    URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();

    // Iterate over all divisors.
    for (int i = 0; i < DIVISORS.length; i++) {
        System.setProperty("divisor", "" + DIVISORS[i]);
        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
        Class c = loader.loadClass("Test6800154");
        Runnable r = (Runnable) c.newInstance();
        r.run();
    }
}