ClassLoaderUtils { @Nonnull public static Set<ClassLoader> getInheritableClassLoaders(ClassLoader classLoader) throws NullPointerException { Set<ClassLoader> classLoadersSet = Sets.newLinkedHashSet(); classLoadersSet.add(classLoader); ClassLoader parentClassLoader = classLoader.getParent(); while (parentClassLoader != null) { classLoadersSet.add(parentClassLoader); parentClassLoader = parentClassLoader.getParent(); } return Collections.unmodifiableSet(classLoadersSet); }  static int getLoadedClassCount(); static long getUnloadedClassCount(); static boolean isVerbose(); static void setVerbose(boolean value); static long getTotalLoadedClassCount(); static Set<Class<?>> findLoadedClasses(ClassLoader classLoader, Set<String> classNames); static boolean isLoadedClass(ClassLoader classLoader, Class<?> type); static boolean isLoadedClass(ClassLoader classLoader, String className); static Class<?> findLoadedClass(ClassLoader classLoader, String className); @Nullable static Class<?> loadClass(@Nonnull ClassLoader classLoader, @Nonnull String className); static Set<URL> getResources(ClassLoader classLoader, ResourceType resourceType, String resourceName); static Set<URL> getResources(ClassLoader classLoader, String resourceName); static URL getResource(ClassLoader classLoader, String resourceName); static URL getResource(ClassLoader classLoader, ResourceType resourceType, String resourceName); static URL getClassResource(ClassLoader classLoader, String className); static URL getClassResource(ClassLoader classLoader, Class<?> type); @Nonnull static Set<ClassLoader> getInheritableClassLoaders(ClassLoader classLoader); @Nonnull static Map<ClassLoader, Set<Class<?>>> getAllLoadedClassesMap(ClassLoader classLoader); @Nonnull static Set<Class<?>> getAllLoadedClasses(ClassLoader classLoader); @Nonnull static Set<Class<?>> getLoadedClasses(ClassLoader classLoader); static Set<Class<?>> findLoadedClassesInClassPath(ClassLoader classLoader); static Set<Class<?>> findLoadedClassesInClassPaths(ClassLoader classLoader, Set<String> classPaths); static Set<Class<?>> findLoadedClassesInClassPath(ClassLoader classLoader, String classPath);  }
@Test public void testFields() throws Exception { List<Field> allFields = FieldUtils.getAllFieldsList(ClassLoader.class); Set<ClassLoader> classLoaders = ClassLoaderUtils.getInheritableClassLoaders(classLoader); for (ClassLoader classLoader : classLoaders) { echo(String.format("ClassLoader : %s", classLoader)); for (Field field : allFields) { if (!Modifier.isStatic(field.getModifiers())) { field.setAccessible(true); String message = String.format("Field name : %s , value : %s", field.getName(), ToStringBuilder.reflectionToString(field.get(classLoader), ToStringStyle.NO_CLASS_NAME_STYLE)); echo(message); } } } }
@Test public void testGetInheritableClassLoaders() { Set<ClassLoader> classLoaders = ClassLoaderUtils.getInheritableClassLoaders(classLoader); Assert.assertNotNull(classLoaders); Assert.assertTrue(classLoaders.size() > 1); echo(classLoaders); }