zoukankan      html  css  js  c++  java
  • classLoader打破双亲委托机制

    思路:

    通过重写loadClass方法将加载类的对象改为自定义类加载器

    目标对象

    package com.dwz.classLoader.chapter4;
    
    public class SimpleObject {
    
    }

    类加载器对象

    package com.dwz.classLoader.chapter4;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    
    public class SimpleClassLoader extends ClassLoader{
        private final static String DEFAULT_DIR = "C:/Users/Administrator/Desktop/revert";
        
        private String dir = DEFAULT_DIR;
        
        private String classLoaderName;
    
        public SimpleClassLoader() {
            super();
        }
        
        public SimpleClassLoader(String classLoaderName) {
            super();
            this.classLoaderName = classLoaderName;
        }
    
        public SimpleClassLoader(String classLoaderName, ClassLoader parent) {
            //父委托
            super(parent);
            this.classLoaderName = classLoaderName;
        }
        
        @Override
        protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            Class<?> clazz = null;
            
            if(name.startsWith("java.")) {
                try {
                    ClassLoader system = ClassLoader.getSystemClassLoader();
                    clazz = system.loadClass(name);
                    if(clazz != null) {
                        if(resolve) {
                            resolveClass(clazz);
                        }
                        return clazz;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
            try {
                clazz = findClass(name);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            if(clazz == null && getParent() != null) {
                getParent().loadClass(name);
            }
            return clazz;
        }
        
        /**
         * @param name (xxx.xxx.xxx.xxx)
         *     xxx/xxx/xxx/xxx.class
         */
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            String classPath = name.replaceAll("\.", "/");
            File classFile = new File(dir, (classPath + ".class"));
            if(!classFile.exists()) {
                throw new ClassNotFoundException("The class " + name + " not found under " + dir);
            }
            
            byte[] classBytes = loadClassBytes(classFile);
            if(null == classBytes || classBytes.length == 0) {
                throw new ClassNotFoundException("load the class " + name + " failed");
            }
            
            return this.defineClass(name, classBytes, 0, classBytes.length);
        }
        
        private byte[] loadClassBytes(File classFile) {
            try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    FileInputStream fis = new FileInputStream(classFile)) {
                byte[] buffer = new byte[1024];
                int len;
                while((len = fis.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                baos.flush();
                return baos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public String getDir() {
            return dir;
        }
    
        public void setDir(String dir) {
            this.dir = dir;
        }
    
        public String getClassLoaderName() {
            return classLoaderName;
        }
    }

    测试

    package com.dwz.classLoader.chapter4;
    
    public class SimpleClassLoaderTest {
        public static void main(String[] args) throws ClassNotFoundException {
            SimpleClassLoader simpleClassLoader = new SimpleClassLoader();
            Class<?> loadClass = simpleClassLoader.loadClass("com.dwz.classLoader.chapter4.SimpleObject");
            System.out.println(loadClass.getClassLoader());
        }
    }
  • 相关阅读:
    word2vec原理(一) CBOW与Skip-Gram模型基础
    条件随机场CRF(三) 模型学习与维特比算法解码
    条件随机场CRF(二) 前向后向算法评估标记序列概率
    linux下如何强制卸载设备?
    docker build时如何使用代理?
    如何配置docker使用代理?
    linux下tar解压时报"gzip: stdin: unexpected end of file"如何处理?
    ubuntu下如何安装valgrind?
    linux下如何睡眠1ms?
    linux用户空间如何控制gpio?
  • 原文地址:https://www.cnblogs.com/zheaven/p/12212938.html
Copyright © 2011-2022 走看看