java类加载原理

java类加载机制
  1. 双亲委派机制
    在这里插入图片描述

  2. 类关系图,APPClassLoader和ExtClassLoader都是继承自URLClassLoader,
    AppClassLoader的继承关系图
    ExtClassLoader继承图
    类加载器AppClassLoader 、ExtClassLoader都是sun.misc.Launcher的静态内部类,BootStrapClassLoader是使用C++实现。

类加载源码
  1. 手动加载一个类,在loadClass位置打个断点方便调试
  public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
        URL[] urls = new URL[1];
        urls[0] = new URL("file:///home/chenkun/VsCodeProjects/tempProject/");
        URLClassLoader classLoader = new URLClassLoader(urls);
        Class<?> hello = classLoader.loadClass("Hello");
    }
  1. 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 {
                    	//ExtClassLoader的parent是null,进入此处执行BootStrapClassLoader,此方法是最终调用一个native方法,是C++实现的,c还是null
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }
				//每次递归c都是null
                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    //,AppClassLoader和ExtClassLoader的loadClass执行到此方法时,最终会抛出ClassNotFoundException,被捕获,捕获后没有做任何处理逻辑,直到URLClassLoader执行findClass(name)方法时得到class,c不再为空。
                    //自定义类加载器一般只需要重写findClass方法即可
                    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;
        }
    }
/**
 *URLClassLoader类的findClass实现
 */
 protected Class<?> findClass(final String name)
        throws ClassNotFoundException
    {
        final Class<?> result;
        try {
            result = AccessController.doPrivileged(
                new PrivilegedExceptionAction<Class<?>>() {
                    public Class<?> run() throws ClassNotFoundException {
                        String path = name.replace('.', '/').concat(".class");
                        Resource res = ucp.getResource(path, false);
                        if (res != null) {
                            try {
                                return defineClass(name, res);
                            } catch (IOException e) {
                                throw new ClassNotFoundException(name, e);
                            }
                        } else {
                            return null;
                        }
                    }
                }, acc);
        } catch (java.security.PrivilegedActionException pae) {
            throw (ClassNotFoundException) pae.getException();
        }
        if (result == null) {
            throw new ClassNotFoundException(name);
        }
        return result;
    }

破坏双亲委派

在这里插入图片描述

类加载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值