JVM- What classes are loaded by various loaders? (class loader sphere) https://yuhongliang.blog.csdn.net/article/details/111566140 class loading process analysis:

LoadClass () loads any class

package com.yuhl.c2020;

/ * * *@author yuhl
 * @Date2020/12/22 use *@Classname LoadClassResource
 * @Description TODO
 */
public class LoadClassResource {
    public static void main(String[] args) throws ClassNotFoundException {
        Class clazz = LoadClassResource.class.getClassLoader().loadClass("com.yuhl.c2020.Miao"); System.out.println(clazz.getName()); }}Copy the code

LoadClass () source code analysis

call

   publicClass<? > loadClass(String name)throws ClassNotFoundException {
        return loadClass(name, false);
    }
Copy the code

LoadClass (String name, Boolean resolve)

// name = com.yuhl.c2020.Miao
protectedClass<? > loadClass(String name,boolean resolve)
        throws ClassNotFoundException
    {
    // The lock is placed before the call, indicating thread-safe
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            // Check whether memory already exists (already loaded)Class<? > c = findLoadedClass(name);// If the cleavage is not loaded, perform the following operations
            if (c == null) {
                long t0 = System.nanoTime();
                try {
	                // If not, the parent member variable of the current class loader is passed up to start the parent delegate
	                // If parent is not empty, parent resets the method
                    if(parent ! =null) {
                        c = parent.loadClass(name, false);
                    } else {
                    	Private native Class
       findBootstrapClass(String name); There are JVM calls to handlec = findBootstrapClassOrNull(name); }}catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

				// If it is still not loaded, there is a findClass () method
                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    FindClass (); /** findClass(); protected Class
       findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); } * /
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    // this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); }}if (resolve) {
                resolveClass(c);
            }
            returnc; }}Copy the code

4. To summarize

  1. Source code to see the parent delegate code implementation
  2. You need to override the findClass() method if you want to customize the classloader

Next article: 024 – the JVM – custom class loader at https://yuhongliang.blog.csdn.net/article/details/111567879