JVM learning (seven) - Adder class parents entrust mechanism

JVM learning (seven) - Adder class parents entrust mechanism

Class loader

Hierarchy among the Java class loader

  • Root class loader
  • Extension class loader
  • System class loader
  • User-defined class loader

This is the topmost layer below the root class loader is the extension class loader and so on.

User-defined load are typically derived from the system class loader.

Father commissioned the class loader mechanism

  • Delegation mechanism in the parent class, each of the loaders in the parent-child relationships forming a tree structure , except the root class loader remaining loader has one and only one parent loader.
  • If a class loader loads successfully Test classes, then the class loader is referred defined class loader , all successfully returned successfully Class object class loader application (including definitions class loader) are referred to as the initial class loading device (understand)

Figure:

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-akxTM3i1-1581322584387) (E: \ Typora \ pictures \ 2020-02-10-01.png)]

** Parents delegation mechanism: ** loader1 want to load the Sample class, is not loaded by loader1 to Sample category among the delegation mechanism according to parent class loader, in fact, it is forwarded to it Sample class system class loader and then to expand class loader to find his father there, will be transferred to the root class loader but the root class loader and can not be loaded.

We can look at the following chart

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-uk7TiDV7-1581322584388) (E: \ Typora \ pictures \ 2020-02-10-02.png)]

** Parents delegation mechanism: ** here say the root class can not fail to load the Sample class so he will return to the extension class loader tries to load and so on, when it is loaded success will return the results to loader1

Bootstrap ClassLoader、Extension ClassLoader、App ClassLoader

  • Bootstrap ClassLoader (root class loader)
    • $ JAVA_HOME in jre / lib / rt.jar in every class, implemented by C ++, not ClassLoader subclass
  • Extension ClassLoader (extended class loader)
    • Responsible for loading the java platform extend some functions jar package, including $ JAVA_HOME in jre / lib / *. Jar jar package or -Djava.ext.dirs specify it in the directory
  • App ClassLoader (the system class loader)
    • 负责加载classpath中指定的jar包及目录中class

接下来我们用代码进行测试

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

    }
}

运行结果:

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-vsbalQ6T-1581322584388) (E: \ Typora \ pictures \ 2020-02-10-04.jpg)]

这里我们可以看一下.getClassLoader()的源代码定义在Class类当中以及一些英文注释后面都接上中文翻译大家都凭自己的理解

   /**
     * Returns the class loader for the class.  Some implementations may use 返回类的类加载器。一些实现可能使用
     * null to represent the bootstrap class loader. This method will return 空表示引导类装入器。此方法将返回
     * null in such implementations if this class was loaded by the bootstrap 如果此类是由引导程序加载的,则在此类实现中为空
     * 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 如果存在安全管理器,并且调用方的类加载器是
     * the class loader for the class whose class loader is requested, then 请求其类加载器的类的类加载器,然后
     * this method calls the security manager's {@code checkPermission} 此方法调用安全管理器的{@code checkPermission}
     * method with a {@code RuntimePermission("getClassLoader")} 具有{@code RuntimePermission(“getClassLoader”)的方法
     * permission to ensure it's ok to access the class loader for the class. 权限,以确保可以访问该类的类加载器。
     *
     * <p>If this object 如果这个对象
     * represents a primitive type or void, null is returned. 表示基元类型或void,返回null。
     *
     * @return  the class loader that loaded the class or interface 加载类或接口的类加载器
     *          represented by this object. 由这个对象表示。
     * @throws SecurityException
     *    if a security manager exists and its 如果存在安全管理器及其
     *    {@code checkPermission} method denies {@code checkPermission}方法拒绝
     *    access to the class loader for 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;
    }

​ 这里的clazz输出为null所以是有Bootstrap ClassLoader(根类加载器)$JAVA_HOME中jre/lib/rt.jar里所有的class因为String是rt.jar里面的class文件,大家可以看看这句翻译(null in such implementations if this class was loaded by the bootstrap 如果此类是由引导程序加载的,则在此类实现中为空

更改代码进行测试

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());//获取类加载器
   }
}


class A {

}

运行结果:

Here Insert Picture Description
​ 那么clazz2的输出sun.misc.Launcher$AppClassLoader@73d16e93,AppClassLoader(应用类加载器)系统类加载器这里证明了A的加载器是AppClassLoader接下来我们来看看这个类里面的代码。

 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 */ }
}


这里的是反编译出来的不是源代码AppClassLoader是我们的应用类加载器(系统类加载器)主要负责我们工程当中的class文件,他是间接从ClassLoader衍生展开的。
发布了13 篇原创文章 · 获赞 2 · 访问量 853

Guess you like

Origin blog.csdn.net/weixin_44281696/article/details/104250369