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

破坏双亲委派

在这里插入图片描述

类加载

猜你喜欢

转载自blog.csdn.net/chen462488588/article/details/113179677
今日推荐