JVM learning classadder parent delegation mechanism

JVM learning (7) - two parent delegation mechanism of class adder

Class loader

The hierarchical system of classloader in Java

  • Root loader
  • extensions class loader
  • system class loader
  • User defined class loader

This is the top-level root class loader, and so on.

User defined loaders are generally derived from system class loaders.

The parent delegation mechanism of classloader

  • In the parent class delegation mechanism, each loader forms a tree structure according to the parent-child relationship. Except for the root class loader, other loaders have only one parent loader.
  • If a classloader can successfully load the Test Class, the classloader is called the definition classloader, and all the classloaders (including the definition classloader) that can successfully return the Class object application are called the initial classloader (understand)

As shown in the picture:

**Parent delegation mechanism: * * loader1 wants to load the Sample class. According to the parent delegation mechanism of the class loader, it is not loaded into the Sample class by loader1. In fact, it is that the Sample class is transferred to the system class loader and then passed to the extension class loader. It is found that it also has a father, and it will be transferred to the root class loader. However, the root class loader cannot load.

Let's see the picture below

**Parent delegation mechanism: * * it is said that the root class cannot load the Sample class, so it will return the extension class loader to try to load and so on. When the load is successful, the result will be returned to loader1

Bootstrap ClassLoader,Extension ClassLoader,App ClassLoader

  • Bootstrap ClassLoader
    • All classes in jre/lib/rt.jar in $JAVA_HOME are implemented by C + +, not ClassLoader subclass
  • Extension ClassLoader
    • Responsible for loading some jar packages of extension functions in Java platform, including jre/lib/*.jar in $JAVA_HOME or jar package in directory specified by - Djava.ext.dirs
  • App ClassLoader (system classloader)
    • Responsible for loading the jar package specified in the classpath and the class in the directory

Next we test with code

public class MyTest7 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> clazz= Class.forName("java.lang.String");
        System.err.println(clazz.getClassLoader());

    }
}

Operation result:

Here we can see that the source code of. getClassLoader() is defined in the Class class and some English comments are followed by Chinese translation. We all have our own understanding

   /**
     * Returns the class loader for the class.  Some implementations may use Returns the class loader for the class. Some implementations may use
     * null to represent the bootstrap class loader. This method will return Null indicates the bootstrap class loader. This method will return
     * null in such implementations if this class was loaded by the bootstrap Empty in this implementation if this class is loaded by the bootstrapper
     * class loader. Class loader
     *
     * <p> If a security manager is present, and the caller's class loader is
     * not null and the caller's class loader is not the same as or an ancestor of If there is a security manager, and the caller's classloader is
     * the class loader for the class whose class loader is requested, then The classloader of the class whose classloader is requested, and
     * this method calls the security manager's {@code checkPermission} This method calls {@ code checkPermission} of the Security Manager
     * method with a {@code RuntimePermission("getClassLoader")} Method with {@ code RuntimePermission ("getClassLoader")
     * permission to ensure it's ok to access the class loader for the class. Permission to ensure access to the class loader for that class.
     *
     * <p>If this object If this object
     * represents a primitive type or void, null is returned. Represents primitive type or void, and returns null.
     *
     * @return  the class loader that loaded the class or interface Class loader to load a class or interface
     *          represented by this object. Represented by this object.
     * @throws SecurityException
     *    if a security manager exists and its If there is a security manager and
     *    {@code checkPermission} method denies {@code checkPermission}Method refusal
     *    access to the class loader for the class. Class loader to access the class.
     * @see java.lang.ClassLoader
     * @see SecurityManager#checkPermission
     * @see java.lang.RuntimePermission
     */
    @CallerSensitive
    public ClassLoader getClassLoader() {
        ClassLoader cl = getClassLoader0();
        if (cl == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
        }
        return cl;
    }

The clazz output here is null, so there are all classes in jre/lib/rt.jar in Bootstrap ClassLoader $JAVA_HOME. Because String is the class file in rt.jar, you can see this translation (null in such implementations if this class was loaded by the bootstrap, it is empty in this implementation)

Change code to test

public class MyTest7 {
   public static void main(String[] args) throws ClassNotFoundException {
       Class<?> clazz= Class.forName("java.lang.String");
       System.err.println(clazz.getClassLoader());
       Class<?> clazz2= Class.forName("com.jvm.classloader.A");
       System.err.println(clazz2.getClassLoader());//Get class loader
   }
}


class A {

}

Operation result:


So the output of clazz2 is sun.misc.Launcher$AppClassLoader@73d16e93. The AppClassLoader system class loader proves that the loader of A is AppClassLoader. Next, let's look at the code in this class.

 static class AppClassLoader extends java.net.URLClassLoader {
        final sun.misc.URLClassPath ucp;

        public static java.lang.ClassLoader getAppClassLoader(java.lang.ClassLoader classLoader) throws java.io.IOException { /* compiled code */ }

        AppClassLoader(java.net.URL[] urls, java.lang.ClassLoader classLoader) { /* compiled code */ }

        public java.lang.Class<?> loadClass(java.lang.String s, boolean b) throws java.lang.ClassNotFoundException { /* compiled code */ }

        protected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource) { /* compiled code */ }

        private void appendToClassPathForInstrumentation(java.lang.String s) { /* compiled code */ }

        private static java.security.AccessControlContext getContext(java.io.File[] files) throws java.net.MalformedURLException { /* compiled code */ }
    }

java.security.AccessControlContext getContext(java.io.File[] files) throws java.net.MalformedURLException { /* compiled code */ }
}

Here is not the source code AppClassLoader, but our application class loader (system class loader), which is mainly responsible for the class files in our project. It is derived from ClassLoader indirectly.
13 original articles published, praised 2, 798 visitors
Private letter follow

Tags: Java jvm

Posted on Mon, 10 Feb 2020 00:51:06 -0800 by Nilpez