BAEL-4302 : How can I list all classes loaded in a specific class loader

This commit is contained in:
root 2020-07-06 16:10:46 +00:00
parent a5f4340e19
commit b2a0fcd76b
6 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package com.baeldung.loadedclasslisting;
public enum ClassLoaderType {
SYSTEM, EXTENSION, BOOTSTRAP, CUSTOM
}

View File

@ -0,0 +1,51 @@
package com.baeldung.loadedclasslisting;
import java.lang.reflect.Method;
import java.util.Arrays;
import com.baeldung.loadedclasslisting.customLoader.ClassLoaderInfo;
import com.baeldung.loadedclasslisting.customLoader.CustomClassLoader;
public class Launcher {
private static ClassLoader customClassLoader;
public static void main(String[] args) {
printClassesLoadedBy(ClassLoaderType.BOOTSTRAP);
printClassesLoadedBy(ClassLoaderType.SYSTEM);
printClassesLoadedBy(ClassLoaderType.EXTENSION);
printClassesLoadedBy(ClassLoaderType.CUSTOM);
}
private static void printClassesLoadedBy(ClassLoaderType classLoaderType) {
Class<?>[] classes;
if (classLoaderType.equals(ClassLoaderType.CUSTOM)) {
customClassLoader = customClassLoading();
classes = ListLoadedClassesAgent.listLoadedClasses(customClassLoader);
} else {
classes = ListLoadedClassesAgent.listLoadedClasses(classLoaderType);
}
Arrays.asList(classes)
.forEach(clazz -> System.out.println(
classLoaderType + " ClassLoader : " + clazz.getCanonicalName()));
}
private static CustomClassLoader customClassLoading() {
CustomClassLoader customClassLoader = new CustomClassLoader();
Class<?> c;
try {
c = customClassLoader.findClass(ClassLoaderInfo.class.getName());
Object ob = c.getDeclaredConstructor()
.newInstance();
Method md = c.getMethod("printClassLoaders");
md.invoke(ob);
} catch (Exception e) {
e.printStackTrace();
}
return customClassLoader;
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.loadedclasslisting;
import java.lang.instrument.Instrumentation;
public class ListLoadedClassesAgent {
private static Instrumentation instrumentation;
public static void premain(String agentArgs, Instrumentation instrumentation) {
ListLoadedClassesAgent.instrumentation = instrumentation;
}
public static Class<?>[] listLoadedClasses(ClassLoaderType classLoaderType) {
if (instrumentation == null) {
throw new IllegalStateException(
"ListLoadedClassesAgent is not initialized.");
}
return instrumentation.getInitiatedClasses(
getClassLoader(classLoaderType));
}
public static Class<?>[] listLoadedClasses(ClassLoader classLoader) {
if (instrumentation == null) {
throw new IllegalStateException(
"ListLoadedClassesAgent is not initialized.");
}
return instrumentation.getInitiatedClasses(classLoader);
}
private static ClassLoader getClassLoader(ClassLoaderType classLoaderType) {
ClassLoader classLoader = null;
switch (classLoaderType) {
case SYSTEM:
classLoader = ClassLoader.getSystemClassLoader();
break;
case EXTENSION:
classLoader = ClassLoader.getSystemClassLoader().getParent();
break;
case BOOTSTRAP:
break;
default:
break;
}
return classLoader;
}
}

View File

@ -0,0 +1 @@
Premain-Class: com.baeldung.loadedclasslisting.ListLoadedClassesAgent

View File

@ -0,0 +1,12 @@
package com.baeldung.loadedclasslisting.customLoader;
import java.util.ArrayList;
public class ClassLoaderInfo {
public void printClassLoaders() throws ClassNotFoundException {
System.out.println("Classloader of this class:" + ClassLoaderInfo.class.getClassLoader());
System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader());
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.loadedclasslisting.customLoader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class CustomClassLoader extends ClassLoader {
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
byte[] b = loadClassFromFile(name);
return defineClass(name, b, 0, b.length);
}
private byte[] loadClassFromFile(String fileName) {
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream(fileName.replace('.', File.separatorChar) + ".class");
byte[] buffer;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int nextValue = 0;
try {
while ((nextValue = inputStream.read()) != -1) {
byteStream.write(nextValue);
}
} catch (IOException e) {
e.printStackTrace();
}
buffer = byteStream.toByteArray();
return buffer;
}
}