Review - II Kevin Gilmore Changes
This commit is contained in:
parent
c1a9c42194
commit
1299a22eb5
@ -1,48 +1,19 @@
|
|||||||
package com.baeldung.loadedclasslisting;
|
package com.baeldung.loadedclasslisting;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import com.baeldung.loadedclasslisting.ListLoadedClassesAgent.ClassLoaderType;
|
|
||||||
import com.baeldung.loadedclasslisting.customLoader.ClassLoaderInfo;
|
|
||||||
import com.baeldung.loadedclasslisting.customLoader.CustomClassLoader;
|
|
||||||
|
|
||||||
public class Launcher {
|
public class Launcher {
|
||||||
|
|
||||||
private static ClassLoader customClassLoader;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
printClassesLoadedBy(ClassLoaderType.BOOTSTRAP);
|
printClassesLoadedBy("BOOTSTRAP");
|
||||||
printClassesLoadedBy(ClassLoaderType.SYSTEM);
|
printClassesLoadedBy("SYSTEM");
|
||||||
printClassesLoadedBy(ClassLoaderType.EXTENSION);
|
printClassesLoadedBy("EXTENSION");
|
||||||
printClassesLoadedBy(ClassLoaderType.CUSTOM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printClassesLoadedBy(ClassLoaderType classLoaderType) {
|
private static void printClassesLoadedBy(String classLoaderType) {
|
||||||
Class<?>[] classes;
|
Class<?>[] classes = ListLoadedClassesAgent.listLoadedClasses(classLoaderType);
|
||||||
if (classLoaderType.equals(ClassLoaderType.CUSTOM)) {
|
|
||||||
customClassLoader = customClassLoading();
|
|
||||||
classes = ListLoadedClassesAgent.listLoadedClasses(customClassLoader);
|
|
||||||
} else {
|
|
||||||
classes = ListLoadedClassesAgent.listLoadedClasses(classLoaderType);
|
|
||||||
}
|
|
||||||
Arrays.asList(classes)
|
Arrays.asList(classes)
|
||||||
.forEach(clazz -> System.out.println(
|
.forEach(clazz -> System.out.println(
|
||||||
classLoaderType + " ClassLoader : " + clazz.getCanonicalName()));
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,13 @@ import java.lang.instrument.Instrumentation;
|
|||||||
|
|
||||||
public class ListLoadedClassesAgent {
|
public class ListLoadedClassesAgent {
|
||||||
|
|
||||||
public enum ClassLoaderType {
|
|
||||||
SYSTEM, EXTENSION, BOOTSTRAP, CUSTOM , PLATFORM
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Instrumentation instrumentation;
|
private static Instrumentation instrumentation;
|
||||||
|
|
||||||
public static void premain(String agentArgs, Instrumentation instrumentation) {
|
public static void premain(String agentArgs, Instrumentation instrumentation) {
|
||||||
ListLoadedClassesAgent.instrumentation = instrumentation;
|
ListLoadedClassesAgent.instrumentation = instrumentation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?>[] listLoadedClasses(ClassLoaderType classLoaderType) {
|
public static Class<?>[] listLoadedClasses(String classLoaderType) {
|
||||||
if (instrumentation == null) {
|
if (instrumentation == null) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"ListLoadedClassesAgent is not initialized.");
|
"ListLoadedClassesAgent is not initialized.");
|
||||||
@ -23,24 +19,18 @@ public class ListLoadedClassesAgent {
|
|||||||
getClassLoader(classLoaderType));
|
getClassLoader(classLoaderType));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?>[] listLoadedClasses(ClassLoader classLoader) {
|
private static ClassLoader getClassLoader(String classLoaderType) {
|
||||||
if (instrumentation == null) {
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"ListLoadedClassesAgent is not initialized.");
|
|
||||||
}
|
|
||||||
return instrumentation.getInitiatedClasses(classLoader);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ClassLoader getClassLoader(ClassLoaderType classLoaderType) {
|
|
||||||
ClassLoader classLoader = null;
|
ClassLoader classLoader = null;
|
||||||
switch (classLoaderType) {
|
switch (classLoaderType) {
|
||||||
case SYSTEM:
|
case "SYSTEM":
|
||||||
classLoader = ClassLoader.getSystemClassLoader();
|
classLoader = ClassLoader.getSystemClassLoader();
|
||||||
break;
|
break;
|
||||||
case EXTENSION:
|
case "EXTENSION":
|
||||||
classLoader = ClassLoader.getSystemClassLoader().getParent();
|
classLoader = ClassLoader.getSystemClassLoader().getParent();
|
||||||
break;
|
break;
|
||||||
case BOOTSTRAP:
|
// passing a null value to the Instrumentation : getInitiatedClasses method
|
||||||
|
// defaults to the bootstrap class loader
|
||||||
|
case "BOOTSTRAP":
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user