自定义类加载器

通过继承ClassLoader类,重写findClass方法,实现自定义类加载器。

一、自定义类加载器

package com.molange.JavaSE.myclassloader;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 编写类继承ClassLoader
 */
public class MyClassLoader extends ClassLoader{
    private String byteCodePath;

    public MyClassLoader( String byteCodePath) {
        this.byteCodePath = byteCodePath;
    }

    public MyClassLoader(ClassLoader parent, String byteCodePath) {
        super(parent);
        this.byteCodePath = byteCodePath;
    }

    /**
     * 通过类的名字,加载指定目录下的文件
     * @param className 类名
     * @return 类的Class文件
     * @throws ClassNotFoundException
     */
    @Override
    protected Class<?> findClass(String className) throws ClassNotFoundException {
        BufferedInputStream bufferedInputStream= null;
        ByteArrayOutputStream byteArrayOutputStream= null;
        try {
            //加载对应文件路径下的class文件
            String fileName=byteCodePath+className+".class";
            //获取当前文件的二进制文件
            bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName));
            //获取一个输出流对象
            byteArrayOutputStream = new ByteArrayOutputStream();
            int len;
            byte[] data=new byte[1024];
            //开始读取二进制流
            while ((len=bufferedInputStream.read(data))!=-1) {
                byteArrayOutputStream.write(data,0,len);
            }
            //将流转换为一个byte类型的数组
            byte[] byteCode=byteArrayOutputStream.toByteArray();
            //将文件全部写入到defineClass中,得到class文件
            Class<?> aClass = defineClass(null, byteCode, 0, byteCode.length);
            return aClass;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {if (byteArrayOutputStream!=null)
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {if (bufferedInputStream!=null)
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }return null;
    }
}

二、测试

package com.molange.JavaSE.myclassloader;

public class MyClassLoaderTest {
    public static void main(String[] args) {
        //加载自定目录下的文件
        MyClassLoader myClassLoader =new MyClassLoader("D:\\IDEA-projects\\JavaSEProjects\\Projects\\src\\com\\molange\\JavaSE\\myclassloader\\");
        try {
            //指定加载文件的类名
            Class<?> classs = myClassLoader.loadClass("MyFather");
            System.out.println("加载此类的类加载器为"+classs.getClassLoader());
            //测试起父类加载器
            System.out.println("当前类的父类将加载器为"+classs.getClassLoader().getParent().getClass().getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

三、测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值