java.lang.ClassLoader.getParent()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(168)

本文整理了Java中java.lang.ClassLoader.getParent()方法的一些代码示例,展示了ClassLoader.getParent()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassLoader.getParent()方法的具体详情如下:
包路径:java.lang.ClassLoader
类名称:ClassLoader
方法名:getParent

ClassLoader.getParent介绍

[英]Returns this class loader's parent.
[中]返回此类装入器的父类。

代码示例

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public boolean matches(T target) {
  ClassLoader current = classLoader;
  while (current != null) {
    if (current == target) {
      return true;
    }
    current = current.getParent();
  }
  return target == null;
}

代码示例来源:origin: commons-logging/commons-logging

public Object run() {
    return cl.getParent();
  }
});

代码示例来源:origin: jenkinsci/jenkins

/**
 * Used for isolated resource seaching.
 * @return the root classloader of AntClassLoader.
 */
private ClassLoader getRootLoader() {
  ClassLoader ret = getClass().getClassLoader();
  while (ret != null && ret.getParent() != null) {
    ret = ret.getParent();
  }
  return ret;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check whether the given ClassLoader is underneath the given parent,
 * that is, whether the parent is within the candidate's hierarchy.
 * @param candidate the candidate ClassLoader to check
 * @param parent the parent ClassLoader to check for
 */
private static boolean isUnderneathClassLoader(@Nullable ClassLoader candidate, @Nullable ClassLoader parent) {
  if (candidate == parent) {
    return true;
  }
  if (candidate == null) {
    return false;
  }
  ClassLoader classLoaderToCheck = candidate;
  while (classLoaderToCheck != null) {
    classLoaderToCheck = classLoaderToCheck.getParent();
    if (classLoaderToCheck == parent) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: redisson/redisson

/**
 * Returns an unsafe class injector for the platform class loader. For VMs of version 8 or older,
 * the extension class loader is represented instead.
 *
 * @return A class injector for the platform class loader.
 */
public static ClassInjector ofPlatformLoader() {
  return new UsingUnsafe(ClassLoader.getSystemClassLoader().getParent());
}

代码示例来源:origin: redisson/redisson

/**
 * Returns a type pool that attempts type descriptions by loadings types from the platform class loader.
 * If the current VM is Java 8 or older, the extension class loader is represented instead.
 *
 * @return An class loading type pool for the system class loader.
 */
public static TypePool ofPlatformLoader() {
  return of(ClassLoader.getSystemClassLoader().getParent());
}

代码示例来源:origin: redisson/redisson

/**
 * Creates a class file locator that queries the plaform class loader or the extension class loader if the
 * current VM is not at least of version 9.
 *
 * @return A class file locator that queries the plaform class loader or the extension class loader.
 */
public static ClassFileLocator ofPlatformLoader() {
  return of(ClassLoader.getSystemClassLoader().getParent());
}

代码示例来源:origin: spring-projects/spring-framework

for (ClassLoader cl = classLoader; cl != null && clazzLoader == null; cl = cl.getParent()) {
  if (instrumentableLoaderClass.isInstance(cl)) {
    clazzLoader = cl;

代码示例来源:origin: google/guava

@VisibleForTesting
static ImmutableMap<File, ClassLoader> getClassPathEntries(ClassLoader classloader) {
 LinkedHashMap<File, ClassLoader> entries = Maps.newLinkedHashMap();
 // Search parent first, since it's the order ClassLoader#loadClass() uses.
 ClassLoader parent = classloader.getParent();
 if (parent != null) {
  entries.putAll(getClassPathEntries(parent));
 }
 for (URL url : getClassLoaderUrls(classloader)) {
  if (url.getProtocol().equals("file")) {
   File file = toFile(url);
   if (!entries.containsKey(file)) {
    entries.put(file, classloader);
   }
  }
 }
 return ImmutableMap.copyOf(entries);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected boolean isEligibleForOverriding(String className) {
  if (isExcluded(className) || ContextTypeMatchClassLoader.this.isExcluded(className)) {
    return false;
  }
  ReflectionUtils.makeAccessible(findLoadedClassMethod);
  ClassLoader parent = getParent();
  while (parent != null) {
    if (ReflectionUtils.invokeMethod(findLoadedClassMethod, parent, className) != null) {
      return false;
    }
    parent = parent.getParent();
  }
  return true;
}

代码示例来源:origin: redisson/redisson

/**
 * Matches exactly the extension {@link java.lang.ClassLoader}. The returned matcher is a synonym to
 * a matcher matching {@code ClassLoader.gerSystemClassLoader().getParent()}.
 *
 * @param <T> The type of the matched object.
 * @return A matcher that only matches the extension class loader.
 */
public static <T extends ClassLoader> ElementMatcher.Junction<T> isExtensionClassLoader() {
  ClassLoader classLoader = ClassLoader.getSystemClassLoader().getParent();
  return classLoader == null // Check if VM supports the extension class loader.
      ? ElementMatchers.<T>none()
      : new EqualityMatcher<T>(classLoader);
}

代码示例来源:origin: redisson/redisson

/**
 * Creates a class file locator for a given class loader. If the class loader is not the bootstrap
 * class loader or the system class loader which cannot be collected, the class loader is only weakly
 * referenced.
 *
 * @param classLoader The class loader to be used. If this class loader represents the bootstrap class
 *                    loader which is represented by the {@code null} value, this system class loader
 *                    is used instead.
 * @return A corresponding source locator.
 */
public static ClassFileLocator of(ClassLoader classLoader) {
  return classLoader == null || classLoader == ClassLoader.getSystemClassLoader() || classLoader == ClassLoader.getSystemClassLoader().getParent()
      ? ForClassLoader.of(classLoader)
      : new WeaklyReferenced(classLoader);
}

代码示例来源:origin: Alluxio/alluxio

private static ClassLoader getClassLoader(Class<?> clazz) {
  // Power Mock makes this hard, so try to hack it
  ClassLoader cl = clazz.getClassLoader();
  if (cl instanceof MockClassLoader) {
   cl = cl.getParent();
  }
  return cl;
 }
}

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public boolean matches(T target) {
  ClassLoader current = target;
  while (current != null) {
    if (matcher.matches(current)) {
      return true;
    }
    current = current.getParent();
  }
  return matcher.matches(null);
}

代码示例来源:origin: jenkinsci/jenkins

private DependencyClassLoader findAncestorDependencyClassLoader(ClassLoader classLoader)
{
  for (; classLoader != null; classLoader = classLoader.getParent()) {
    if (classLoader instanceof DependencyClassLoader) {
      return (DependencyClassLoader)classLoader;
    }
    
    if (classLoader instanceof AntClassLoader) {
      // AntClassLoaders hold parents not only as AntClassLoader#getParent()
      // but also as AntClassLoader#getConfiguredParent()
      DependencyClassLoader ret = findAncestorDependencyClassLoader(
          ((AntClassLoader)classLoader).getConfiguredParent()
      );
      if (ret != null) {
        return ret;
      }
    }
  }
  return null;
}

代码示例来源:origin: org.springframework/spring-context

@Override
protected boolean isEligibleForOverriding(String className) {
  if (isExcluded(className) || ContextTypeMatchClassLoader.this.isExcluded(className)) {
    return false;
  }
  ReflectionUtils.makeAccessible(findLoadedClassMethod);
  ClassLoader parent = getParent();
  while (parent != null) {
    if (ReflectionUtils.invokeMethod(findLoadedClassMethod, parent, className) != null) {
      return false;
    }
    parent = parent.getParent();
  }
  return true;
}

代码示例来源:origin: prestodb/presto

@VisibleForTesting
static ImmutableMap<File, ClassLoader> getClassPathEntries(ClassLoader classloader) {
 LinkedHashMap<File, ClassLoader> entries = Maps.newLinkedHashMap();
 // Search parent first, since it's the order ClassLoader#loadClass() uses.
 ClassLoader parent = classloader.getParent();
 if (parent != null) {
  entries.putAll(getClassPathEntries(parent));
 }
 for (URL url : getClassLoaderUrls(classloader)) {
  if (url.getProtocol().equals("file")) {
   File file = toFile(url);
   if (!entries.containsKey(file)) {
    entries.put(file, classloader);
   }
  }
 }
 return ImmutableMap.copyOf(entries);
}

代码示例来源:origin: google/guava

private WeakReference<ClassLoader> doTestUnloadableInStaticFieldIfClosed() throws Exception {
 final ClassLoader myLoader = getClass().getClassLoader();
 URLClassLoader sepLoader = new URLClassLoader(getClassPathUrls(), 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);
}

代码示例来源:origin: redisson/redisson

/**
 * Creates a class file locator for a Java module where the module is referenced weakly. If the module is not named, the module's class loader
 * is represented instead. Module's of the boot layer are not referenced weakly.
 *
 * @param module The Java module to represent.
 * @return A suitable class file locator.
 */
public static ClassFileLocator of(JavaModule module) {
  if (module.isNamed()) {
    return module.getClassLoader() == null || module.getClassLoader() == ClassLoader.getSystemClassLoader() || module.getClassLoader() == ClassLoader.getSystemClassLoader().getParent()
        ? new ForModule(module)
        : new WeaklyReferenced(module.unwrap());
  } else {
    return ForClassLoader.WeaklyReferenced.of(module.getClassLoader());
  }
}

代码示例来源:origin: apache/incubator-druid

public static void assertClassLoaderIsSingular(ClassLoader classLoader)
 {
  // This is a check against the current HadoopTask which creates a single URLClassLoader with null parent
  Assert.assertNull(classLoader.getParent());
  Assert.assertFalse(classLoader instanceof ApplicationClassLoader);
  Assert.assertTrue(classLoader instanceof URLClassLoader);

  final ClassLoader appLoader = HadoopDruidConverterConfig.class.getClassLoader();
  Assert.assertNotEquals(StringUtils.format("ClassLoader [%s] is not isolated!", classLoader), appLoader, classLoader);
 }
}

相关文章