java中的classloader的原理

最近在琢磨btrace,顺便也看了下JDK的classloader的原理

先直接把代码贴出来:
首先是main方法:
package com.gao;

import com.wang.MyClass;

/**
 * User: wangchen.gpx
 * Date: 12-12-28
 * Time: 上午9:35
 */
public class ClassLoaderTeste {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        MyClassLoader myclassloader = new MyClassLoader("myclassloader");
        Class<?> myClass = myclassloader.loadClass("com.wang.MyClass");
        Object o = myClass.newInstance();
        MyClass myClass1 = (MyClass) o;
        myClass1.say();
    }
}

然后是自定义的classloader
package com.gao;

import java.io.*;

/**
 * User: wangchen.gpx
 * Date: 12-12-28
 * Time: 上午9:26
 */
public class MyClassLoader extends ClassLoader {
    private String loaderName;
    private String path = "G:\\idea_workspace\\asmTest\\out\\production\\asmTest\\java\\java\\";

    public MyClassLoader(String loaderName) {
        this.loaderName = loaderName;
    }

    public MyClassLoader(ClassLoader parent, String loaderName) {
        super(parent);
        this.loaderName = loaderName;
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return this.findClass(name);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        System.out.println("find class");
        byte[] bytes = this.loadClassData(name);
        return this.defineClass(name, bytes, 0, bytes.length);
    }

    private byte[] loadClassData(String name) {
        try {
            name = name.replace(".", "//");
            FileInputStream fileInputStream = new FileInputStream(new File(path + name + ".class"));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int b  = 0;
            while ((b = fileInputStream.read()) != -1) {
                byteArrayOutputStream.write(b);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

最后是用来装载的class:
package com.wang;

/**
 * User: wangchen.gpx
 * Date: 12-12-28
 * Time: 上午9:08
 */
public class MyClass {
    public MyClass() {
        System.out.println("constructor");
    }

    public void say(){
        System.out.println("one");
    }
}


这里首先跑的话,我认为应该是直接走自己的classloader的findclass方法,但是我错了,根本就没有走到这里,所以就比较纳闷,自己看了下classloader的源码,原来是这样:
protected synchronized Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException
    {
        // First, check if the class has already been loaded
        //这里首先查看该类是否已经装载了,像我们自定义的类,现在还没有装载,因此这里应该返回的是null
        Class c = findLoadedClass(name);
        if (c == null) {
            try {
                if (parent != null) {
                    //注意这里,他首先会用父classloader去装载,父类classloader也会调用它的
                    //父类classloader进行装载,已经递归下去,直到进入bootstrapclassloader
                    //如果bootstrapclassloader找不到该类,那么他将委托为appClassloader去装载
                    //这个就是所说的双亲委派模型
                    c = parent.loadClass(name, false);
                } else {
                    //这一步就是最后到达的bootstrapclassloader
                    c = findBootstrapClassOrNull(name);
                }
            } catch (ClassNotFoundException e) {
                // ClassNotFoundException thrown if class not found
                // from the non-null parent class loader
            }
            //注意这里,只有真的找不到的时候才会进入这一步,调用自己实现的findclass方法来loadclass
            if (c == null) {
                // If still not found, then invoke findClass in order
                // to find the class.
                c = findClass(name);
            }
        }
        if (resolve) {
            resolveClass(c);
        }
        return c;
    }


所以这次又对类加载的机制有了一定的了解,主要是双亲委派模型的实际含义

你可能感兴趣的:(ClassLoader)