Arthas

Arthas classloader类加载器

2021-05-16  本文已影响0人  晴天哥_王志

系列

开篇

classloader查找流程

public class ClassLoaderCommand extends AnnotatedCommand {

    private void getAllClasses(String hashCode, Instrumentation inst, RowAffect affect, CommandProcess process) {
        int hashCodeInt = -1;
        if (hashCode != null) {
            hashCodeInt = Integer.valueOf(hashCode, 16);
        }
        // bootstrapClassSet保存class
        SortedSet<Class<?>> bootstrapClassSet = new TreeSet<Class<?>>(new Comparator<Class>() {
            @Override
            public int compare(Class o1, Class o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        // 通过inst.getAllLoadedClasses获取所有加载的类
        Class[] allLoadedClasses = inst.getAllLoadedClasses();
        // classLoaderClassMap保存类加载器和对应的class
        Map<ClassLoader, SortedSet<Class<?>>> classLoaderClassMap = new HashMap<ClassLoader, SortedSet<Class<?>>>();
        for (Class clazz : allLoadedClasses) {
            // 获取类对应的类加载器
            ClassLoader classLoader = clazz.getClassLoader();
            // Class loaded by BootstrapClassLoader
            if (classLoader == null) {
                if (hashCode == null) {
                    bootstrapClassSet.add(clazz);
                }
                continue;
            }

            if (hashCode != null && classLoader.hashCode() != hashCodeInt) {
                continue;
            }

            SortedSet<Class<?>> classSet = classLoaderClassMap.get(classLoader);
            if (classSet == null) {
                classSet = new TreeSet<Class<?>>(new Comparator<Class<?>>() {
                    @Override
                    public int compare(Class<?> o1, Class<?> o2) {
                        return o1.getName().compareTo(o2.getName());
                    }
                });
                classLoaderClassMap.put(classLoader, classSet);
            }
            classSet.add(clazz);
        }
    }
}
public class ClassLoaderVO {
    private String name;
    private String hash;
    private String parent;
    private Integer loadedCount;
    private Integer numberOfInstances;
    private List<ClassLoaderVO> children;

    public ClassLoaderVO() {
    }

    public void addChild(ClassLoaderVO child){
        if (this.children == null){
            this.children = new ArrayList<ClassLoaderVO>();
        }
        this.children.add(child);
    }
}
上一篇下一篇

猜你喜欢

热点阅读