当前位置: 首页>>代码示例>>Java>>正文


Java URLClassLoader.getParent方法代码示例

本文整理汇总了Java中java.net.URLClassLoader.getParent方法的典型用法代码示例。如果您正苦于以下问题:Java URLClassLoader.getParent方法的具体用法?Java URLClassLoader.getParent怎么用?Java URLClassLoader.getParent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLClassLoader的用法示例。


在下文中一共展示了URLClassLoader.getParent方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testShadow

import java.net.URLClassLoader; //导入方法依赖的package包/类
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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:19,代码来源:LeakTest.java

示例2: doTestUnloadableInStaticFieldIfClosed

import java.net.URLClassLoader; //导入方法依赖的package包/类
private WeakReference<ClassLoader> doTestUnloadableInStaticFieldIfClosed() throws Exception {
  final URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  final URL[] urls = myLoader.getURLs();
  URLClassLoader sepLoader = new URLClassLoader(urls, myLoader.getParent());

  Class<?> frqC = FinalizableReferenceQueue.class;
  Class<?> sepFrqC = sepLoader.loadClass(frqC.getName());
  assertNotSame(frqC, sepFrqC);

  Class<?> sepFrqSystemLoaderC =
      sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName());
  Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled");
  disabled.setAccessible(true);
  disabled.set(null, true);

  Class<?> frqUserC = FrqUser.class;
  Class<?> sepFrqUserC = sepLoader.loadClass(frqUserC.getName());
  assertNotSame(frqUserC, sepFrqUserC);
  assertSame(sepLoader, sepFrqUserC.getClassLoader());

  Callable<?> sepFrqUser = (Callable<?>) sepFrqUserC.newInstance();
  WeakReference<?> finalizableWeakReference = (WeakReference<?>) sepFrqUser.call();

  GcFinalization.awaitClear(finalizableWeakReference);

  Field sepFrqUserFinalizedF = sepFrqUserC.getField("finalized");
  Semaphore finalizeCount = (Semaphore) sepFrqUserFinalizedF.get(null);
  boolean finalized = finalizeCount.tryAcquire(5, TimeUnit.SECONDS);
  assertTrue(finalized);

  Field sepFrqUserFrqF = sepFrqUserC.getField("frq");
  Closeable frq = (Closeable) sepFrqUserFrqF.get(null);
  frq.close();

  return new WeakReference<ClassLoader>(sepLoader);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:37,代码来源:FinalizableReferenceQueueClassLoaderUnloadingTest.java

示例3: main

import java.net.URLClassLoader; //导入方法依赖的package包/类
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();
    }
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:15,代码来源:Test6800154.java

示例4: main

import java.net.URLClassLoader; //导入方法依赖的package包/类
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();
    }
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:15,代码来源:Test6805724.java

示例5: loadandrunclass

import java.net.URLClassLoader; //导入方法依赖的package包/类
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();
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:9,代码来源:Test6823354.java

示例6: loadAndRunClass

import java.net.URLClassLoader; //导入方法依赖的package包/类
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();
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:9,代码来源:Test5057225.java

示例7: loadandrunclass

import java.net.URLClassLoader; //导入方法依赖的package包/类
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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:Test6823354.java

示例8: useFrqInSeparateLoader

import java.net.URLClassLoader; //导入方法依赖的package包/类
private WeakReference<ClassLoader> useFrqInSeparateLoader() throws Exception {
  final URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  final URL[] urls = myLoader.getURLs();
  URLClassLoader sepLoader = new URLClassLoader(urls, myLoader.getParent());
  // sepLoader is the loader that we will use to load the parallel FinalizableReferenceQueue (FRQ)
  // and friends, and that we will eventually expect to see garbage-collected. The assumption
  // is that the ClassLoader of this test is a URLClassLoader, and that it loads FRQ itself
  // rather than delegating to a parent ClassLoader. If this assumption is violated the test will
  // fail and will need to be rewritten.

  Class<?> frqC = FinalizableReferenceQueue.class;
  Class<?> sepFrqC = sepLoader.loadClass(frqC.getName());
  assertNotSame(frqC, sepFrqC);
  // Check the assumptions above.

  // FRQ tries to load the Finalizer class (for the reference-collecting thread) in a few ways.
  // If the class is accessible to the system ClassLoader (ClassLoader.getSystemClassLoader())
  // then FRQ does not bother to load Finalizer.class through a separate ClassLoader. That happens
  // in our test environment, which foils the purpose of this test, so we disable the logic for
  // our test by setting a static field. We are changing the field in the parallel version of FRQ
  // and each test creates its own one of those, so there is no test interference here.
  Class<?> sepFrqSystemLoaderC =
      sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName());
  Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled");
  disabled.setAccessible(true);
  disabled.set(null, true);

  // Now make a parallel FRQ and an associated FinalizableWeakReference to an object, in order to
  // exercise some classes from the parallel ClassLoader.
  AtomicReference<Object> sepFrqA = new AtomicReference<Object>(sepFrqC.newInstance());
  Class<?> sepFwrC = sepLoader.loadClass(MyFinalizableWeakReference.class.getName());
  Constructor<?> sepFwrCons = sepFwrC.getConstructor(Object.class, sepFrqC);
  // The object that we will wrap in FinalizableWeakReference is a Stopwatch.
  Class<?> sepStopwatchC = sepLoader.loadClass(Stopwatch.class.getName());
  assertSame(sepLoader, sepStopwatchC.getClassLoader());
  AtomicReference<Object> sepStopwatchA =
      new AtomicReference<Object>(sepStopwatchC.getMethod("createUnstarted").invoke(null));
  AtomicReference<WeakReference<?>> sepStopwatchRef = new AtomicReference<WeakReference<?>>(
      (WeakReference<?>) sepFwrCons.newInstance(sepStopwatchA.get(), sepFrqA.get()));
  assertNotNull(sepStopwatchA.get());
  // Clear all references to the Stopwatch and wait for it to be gc'd.
  sepStopwatchA.set(null);
  GcFinalization.awaitClear(sepStopwatchRef.get());
  // Return a weak reference to the parallel ClassLoader. This is the reference that should
  // eventually become clear if there are no other references to the ClassLoader.
  return new WeakReference<ClassLoader>(sepLoader);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:48,代码来源:FinalizableReferenceQueueClassLoaderUnloadingTest.java

示例9: getClasspathComponents

import java.net.URLClassLoader; //导入方法依赖的package包/类
/**
   * Returns the classpath as a list directory and archive names.
   *
   * @return the classpath as a list of directory and archive file names; if
   *         no components can be found then an empty list will be returned
   */
  public static List getClasspathComponents()
  {
      List components = new LinkedList();

      // walk the classloader hierarchy, trying to get all the components we can
      ClassLoader cl = Thread.currentThread().getContextClassLoader();

      while ((null != cl) && (cl instanceof URLClassLoader))
      {
          URLClassLoader ucl = (URLClassLoader) cl;
          components.addAll(getUrlClassLoaderClasspathComponents(ucl));

          try
          {
              cl = ucl.getParent();
          }
          catch (SecurityException se)
          {
              cl = null;
          }
      }

      // walking the hierarchy doesn't guarantee we get everything, so
      // lets grab the system classpath for good measure.
      String classpath = System.getProperty("java.class.path");
      String separator = System.getProperty("path.separator");
      StringTokenizer st = new StringTokenizer(classpath, separator);
      while (st.hasMoreTokens())
      {
          String component = st.nextToken();
          // Calling File.getPath() cleans up the path so that it's using
          // the proper path separators for the host OS
          component = getCanonicalPath(component);
          log.debug("System classpath: " + component);
          components.add(component);
      }
      
      // search jars in current directory as this library
URL url = ClasspathUtils.class.getProtectionDomain().getCodeSource().getLocation();
String path = url.getPath();
String realPath = path.substring(path.indexOf(':') + 1);
File thisJar = new File(realPath);
if (thisJar != null) {
	File currentDir = thisJar.getParentFile();
	if (currentDir.isDirectory()) {
		File[] jars = currentDir.listFiles(new JarFilter());
		for (File jar : jars) {
			try {
				components.add(jar.getCanonicalPath());
			} catch (IOException e) {
				log.error(e.getMessage());
				e.printStackTrace();
			}
		}
	}
}
 	
      // Set removes any duplicates, return a list for the api.
      return new LinkedList(new HashSet(components));
  }
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:67,代码来源:ClasspathUtils.java


注:本文中的java.net.URLClassLoader.getParent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。