实现自定义类加载器(自定义文件系统类加载器)

本文介绍了如何实现一个自定义的类加载器,该加载器专注于从特定的文件系统路径加载类。通过这个类加载器,可以扩展Java应用程序的类查找机制。
摘要由CSDN通过智能技术生成

实现自定义类加载器(自定义文件系统类加载器)

package cn.com.lodar;


import java.io.*;

public class FileSystemClassLoader extends ClassLoader{
    //自定义文件系统类加载器
    private String rootDir;
    public FileSystemClassLoader(String rootDir) {
        this.rootDir = rootDir;
    }
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        Class<?> c = findLoadedClass(name);//查找这个类
        //查找这个类是否被加载过,如果有,直接返回加载好的类,如果没有,加载新的类
        if (c != null) {
            return c;
        }else {
            ClassLoader parent = this.getParent();//获得父类的加载器
            try {
                c = parent.loadClass(name); //委派给父类加载
            }catch(Exception e) {
//                e.printStackTrace();
            }
//            c = parent.loadClass(name);
            if (c != null) {
                return c;
            }else {
                byte[] classData = getClassData(name);
                if (classData == null) {
                    throw new ClassNotFoundException();
                }else {
                    c = defineClass(name,classData,0,classData.length);
                }
            }
        }
        return c;
    }
    private byte[] getClassData(String classname){
        String path = rootDir+"/"+classname.replace('.','/')+".class";
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            is = new FileInputStream(path);
            baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int tmp;
            while((tmp = is.read(bytes)) != -1) {
                baos.write(bytes,0,tmp);
            }
            baos.flush();
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

测试用例

package cn.com.lodar;

public class TestFileSystemClassLoader {
    public static void main(String[] args) throws ClassNotFoundException {
        FileSystemClassLoader loader = new FileSystemClassLoader("F:/myjava");
        FileSystemClassLoader loader1 = new FileSystemClassLoader("F:/myjava");
        Class<?> c = loader.loadClass("cn.com.test.HelloWord");
        Class<?> c2 = loader.loadClass("cn.com.test.HelloWord");
        Class<?> c3 = loader1.loadClass("cn.com.test.HelloWord");
        Class<?> c4 = loader1.loadClass("java.lang.String");
        Class<?> c5 = loader1.loadClass("cn.com.lodar.Demo02");
//        System.out.println(c);
        System.out.println(c.hashCode());//同一个类加载器加载同一个类,JVM才认为是同一个类
        System.out.println(c2.hashCode());
        System.out.println(c3.hashCode());//不是同一个类加载器加载同一个类,JVM认为是不同的类
        System.out.println(c4.hashCode());
        System.out.println(c3.getClassLoader()); //自定义类加载器
        System.out.println(c4.getClassLoader()); //引导类加载器
        System.out.println(c5.getClassLoader()); //系统默认的类加载器
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值