用戶自定義類加載器探祕

用戶自定義類加載器探祕

MyClassLoader

/**
 * 自定義類加載器
 *
 * @name: MyClassLoader
 * @author: terwer
 * @date: 2022-07-07 21:37
 **/
public class MyClassLoader extends ClassLoader {
    //  類加載器的名字
    private String name;

    // 加載類的路徑
    private String path = "/home/terwer/Downloads";

    // class文件的擴展名
    private final String fileType = ".class";

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public MyClassLoader(String name) {
        // 讓系統類加載器成爲類加載器的父類加載器
        super();
        this.name = name;
    }

    public MyClassLoader(ClassLoader parent, String name) {
        // 顯式指定類的父類加載器
        super(parent);
        this.name = name;
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = this.loadClassData(name);

        return this.defineClass(name, data, 0, data.length);
    }

    private byte[] loadClassData(String name) {
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;

        try {
            this.name = this.name.replace(".", "/");
            is = new FileInputStream(path + "/" + name + fileType);

            baos = new ByteArrayOutputStream();

            int ch = 0;
            while (-1 != (ch = is.read())) {
                baos.write(ch);
            }

            data = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        return data;
    }
}

Main

/**
 * @name: Main
 * @author: terwer
 * @date: 2022-07-07 21:33
 **/
public class Main {
    public static void main(String[] args) throws Exception{
        MyClassLoader loader1 = new MyClassLoader("loader1");
        loader1.setPath("/home/terwer/Downloads/f1");

        MyClassLoader loader2 = new MyClassLoader(loader1, "loader2");
        loader2.setPath("/home/terwer/Downloads/f2");

        MyClassLoader loader3 = new MyClassLoader(null, "loader3");
        loader3.setPath("/home/terwer/Downloads/f3");

        test(loader2);
        test(loader3);
    }

    public static void test(ClassLoader loader) throws Exception {
        Class clazz = loader.loadClass("com.terwergreen.loader.Simple");
        Object object = clazz.getDeclaredConstructor().newInstance();
    }
}

運行效果

image-20220707224506215

分析

image-20220707222253906

image-20220707223251502

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章