jvm双亲委派机制之打破双亲委派机制

在上一篇博客中介绍了一下如何书写自定义加载器.传送门
这一篇要介绍的就是自定义类加载器来打破双亲委派机制.
思路就是加载类的时候不走parent属性来找,直接从指定的classpath中找

package com.bonc.jvm;

import java.io.FileInputStream;
import java.lang.reflect.Method;

/**
 * @Classname MyClassLoadTEst
 * @Description TODO
 * @Date 2020/6/14 16:31
 * @Created by sz
 */
public class MyClassLoadTest {

    static  class  MyClassLoad  extends  ClassLoader{

        private  String classPath;


        public MyClassLoad(String classPath){
            this.classPath=classPath;
        }

        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();
                    //只有自己写的类走自己写的类加载器,原来的类走自己类加载机制 这行代码非常关键
                    if (!name.startsWith("com.bonc")){
                       c = super.loadClass(name,resolve);
                    }
                    if (c == null) {
                        // If still not found, then invoke findClass in order
                        // to find the class.
                        long t1 = System.nanoTime();
                        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;
            }
        }

        private byte[] loadByte(String name) throws Exception {
            name = name.replaceAll("\\.", "/");
            FileInputStream fis = new FileInputStream(classPath + "/" + name
                    + ".class");
            int len = fis.available();
            byte[] data = new byte[len];
            fis.read(data);
            fis.close();
            return data;
        }
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            try {
                byte[] data = loadByte(name);
                //defineClass将一个字节数组转为Class对象,这个字节数组是class文件读取后最终的字节 数组。
                return defineClass(name, data, 0, data.length);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ClassNotFoundException();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        MyClassLoad classLoader = new MyClassLoad("D:/tmp");
        Class clazz = classLoader.loadClass("com.bonc.jvm.User1");
        Object obj = clazz.newInstance();
        Method method = clazz.getDeclaredMethod("print", null);
        method.invoke(obj,null);
        System.out.println(clazz.getClassLoader().getClass().getName());
        System.out.println(clazz.getClassLoader().getParent().getClass().getName());
        System.out.println(clazz.getClassLoader().getParent().getParent().getClass().getName());
    }

}

jvm双亲委派机制之打破双亲委派机制_第1张图片
代码运行效果
jvm双亲委派机制之打破双亲委派机制_第2张图片
在不打破双亲委派机制之前的代码执行第一个的类加载器是AppClassLoader而不是自定义类加载器.因为你的classpath目录下已经存在一个Use1了.
那么我们是否能够重写一个java.lang.String呢

package com.bonc.jvm;

import java.io.FileInputStream;
import java.lang.reflect.Method;

/**
 * @Classname MyClassLoadTEst
 * @Description TODO
 * @Date 2020/6/14 16:31
 * @Created by sz
 */
public class MyClassLoadTest {

    static  class  MyClassLoad  extends  ClassLoader{

        private  String classPath;


        public MyClassLoad(String classPath){
            this.classPath=classPath;
        }

        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();

                    if (c == null) {
                        // If still not found, then invoke findClass in order
                        // to find the class.
                        long t1 = System.nanoTime();
                        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;
            }
        }

        private byte[] loadByte(String name) throws Exception {
            name = name.replaceAll("\\.", "/");
            FileInputStream fis = new FileInputStream(classPath + "/" + name
                    + ".class");
            int len = fis.available();
            byte[] data = new byte[len];
            fis.read(data);
            fis.close();
            return data;
        }
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            try {
                byte[] data = loadByte(name);
                //defineClass将一个字节数组转为Class对象,这个字节数组是class文件读取后最终的字节 数组。
                return defineClass(name, data, 0, data.length);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ClassNotFoundException();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        MyClassLoad classLoader = new MyClassLoad("D:/tmp");
        Class clazz = classLoader.loadClass("java.lang.String");
        Object obj = clazz.newInstance();
        Method method = clazz.getDeclaredMethod("print", null);
        method.invoke(obj,null);
        System.out.println(clazz.getClassLoader().getClass().getName());
        System.out.println(clazz.getClassLoader().getParent().getClass().getName());
        System.out.println(clazz.getClassLoader().getParent().getParent().getClass().getName());
    }

}

运行结果
jvm双亲委派机制之打破双亲委派机制_第3张图片
结论:java为了保护自己的核心类库被篡权,核心的包下面的类不能被重写

你可能感兴趣的:(jvm,类加载机制)