JVM学习之自定义加载器

由于加载器是父类继承机制,所以在创建自定义加载器之时,需要指明其父类是谁,默认是系统类加载器。

我把ClassLoader部分源码贴一下:

JVM学习之自定义加载器

protected Class<?> loadClass(String name, boolean resolve)
    throws ClassNotFoundException
{
    synchronized (getClassLoadingLock(name)) {
        // First, check if the class has already been loaded
        Class<?> c = findLoadedClass(name);//先查看是否被加载
        if (c == null) {//如果没有被加载,这调用父类加载器加载
            long t0 = System.nanoTime();
            try {
                if (parent != null) {
                    c = parent.loadClass(name, false);
                } else {
                    c = findBootstrapClassOrNull(name);
                }
            } catch (ClassNotFoundException e) {
                // ClassNotFoundException thrown if class not found
                // from the non-null parent class loader
            }

            if (c == null) {
                // If still not found, then invoke findClass in order
                // to find the class.
                long t1 = System.nanoTime();
                c = findClass(name);

                // this is the defining class loader; record the stats
                sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                sun.misc.PerfCounter.getFindClasses().increment();
            }
        }
        if (resolve) {
            resolveClass(c);
        }
        return c;
    }
}

 

看红色字体,如果父类没有找到则调用findClass来找到这个类,所以咱们做的有二:

1.创建一个findClass方法来找到这个类。

2.把文件移到一个父类加载器找不到的地方,不能是当前项目下(系统加载器能加载),我这里把它移到桌面了。

代码如下(方便大家用手机看,就不写代码块了):

import java.io.*;

public class JVMTest1 extends ClassLoader{

    private String classLoaderName;//为加载器起个名字

    private final  String fileExtension = ".class";//.Class扩展名

    private String path;//设置文件路径,目的是让父类加载器找不到,才能使自定义的加载器生效

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

    public JVMTest1(String classLoaderName){
        super();
        this.classLoaderName = classLoaderName;
    }

    public JVMTest1(ClassLoader parent, String classLoaderName){
        super(parent);
        this.classLoaderName = classLoaderName;
    }

    @Override//The <a href="#name">binary name</a> of the class 二进制名:即类似cn.com.test,包名以点分隔
    protected Class<?> findClass(String className){//加载器在调用loadClass时,loadClass会在底层调用该方法
        byte[] data = this.loadClassData(className);//调用方法,将该文件已字节形式数组的形式放回

        return this.defineClass(className,data,0, data.length);//返回回去
    }

    private byte[] loadClassData(String className) {
        FileInputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;
        try {
            className = className.replace(".","\\");//将包名中的点改为\\(Windows系统)或/(linux系统),便于找到文件位置
            is = new FileInputStream(new File(this.path+className+ this.fileExtension));
            baos = new ByteArrayOutputStream();
            int ch = 0;
            while((ch = is.read())!=-1){
                baos.write(ch);
            }
            data = baos.toByteArray();//以上将文件内容转为字节数组

        } catch (Exception ex){
            ex.printStackTrace();
        }finally {
            try{
                is.close();
                baos.close();
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return data;
    }

    public static void main(String[] args) throws Exception {
        JVMTest1 loader1 = new JVMTest1("loader1");//创建加载器
        loader1.setPath("C:\\Users\\admin\\Desktop\\");//文件位置
        Class<?> clazz = loader1.loadClass("ListTest");//文件名
        Object object = clazz.newInstance();//创建加载器实例
        System.out.println(object.getClass().getClassLoader());

    }

}

JVM学习之自定义加载器

提示:你要加载的这个.Class文件一定要删除,因为加载器采用父亲委托机制,如果父亲发现这个文件在他管辖范围内能找到,自己就加载了,咱们创建的方法,根本不会运行。