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;
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值