/** * {@inheritDoc} */ public boolean matches(T target) { ClassLoader current = classLoader; while (current != null) { if (current == target) { return true; } current = current.getParent(); } return target == null; }
public Object run() { return cl.getParent(); } });
/** * 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; }
/** * 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()); }
/** * 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()); }
/** * 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()); }
for (ClassLoader cl = classLoader; cl != null && clazzLoader == null; cl = cl.getParent()) { if (instrumentableLoaderClass.isInstance(cl)) { clazzLoader = cl;
@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); }
@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; }
/** * 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); }
/** * 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); }
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; } }
/** * {@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); }
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; }
@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; }
@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); }
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); }
/** * 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()); } }
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); } }