jvm Classload method介绍

1,jvm Classload默认几个重要方法介绍
findClass:Finds and loads the class with the specified name from the URL search path.找到class文件并把字节码载入到内存中,假设自己定义的载入器仅覆盖了findClass,而未覆盖loadClass(即载入规则一样,但载入路径不同);则调用getClass().getClassLoader()返回的仍然是AppClassLoader!由于真正load类的,还是AppClassLoader
defineClass:Converts an array of bytes into an instance of class. 创建类对象,将字节流解析成JVM可以识别的Class对象
loadClass:Loads the class with the specified binary name.自己定义的载入器能够覆盖该方法loadClass(),以便定义不同的载入机制.比如Servlet中的WebappClassLoader覆盖了该方法,在WEB-INFO/classes文件夹下查找类文件;在载入时,假设成功,则缓存到ResourceEntry对象。——不同的载入机制。

2,其他一些方法
resolveClass:载入完字节码后,会依据须要进行验证、解析
defineClass:字节流解析成JVM可以识别的Class对象。不可覆盖
findLoadedClass:假设类已经载入过,则直接返回 Returns the class with the given name binary name if this loader has been recorded by the Java virtual machine as an initiating

3,调用规则:loadClass-->findClass-->defineClass--->resolveClass(可选)
4,JVM默认不能热部署类,由于载入类时会去调用findLoadedClass(),假设类已被载入,就不会再次载入。JVM推断类是否被载入有两个条件:完整类名是否一样、ClassLoader是否是同一个。



loadClass方法代码:
   
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;
        }
    }


posted @ 2015-02-05 21:56  blfshiye  阅读(155)  评论(0编辑  收藏  举报