欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

classLoader自定义类加载器

程序员文章站 2022-07-03 14:27:59
...

 下面是实现的案例,参考地址:https://www.cnblogs.com/szlbm/p/5504631.html

注意点:这个名称为class文件的全路径

classLoader自定义类加载器

 

package info.lumanman003;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class MyClassLoader extends ClassLoader
{
    public MyClassLoader()
    {
        
    }
    
    public MyClassLoader(ClassLoader parent)
    {
        super(parent);
    }
    
    protected Class<?> findClass(String name) throws ClassNotFoundException
    {
        File file = getClassFile(name);
        try
        {
            byte[] bytes = getClassBytes(file);
            Class<?> c = this.defineClass(name, bytes, 0, bytes.length);
            return c;
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return super.findClass(name);
    }
    
    private File getClassFile(String name)
    {
        File file = new File("C:\\Users\\WM\\Desktop\\info\\lumanman003\\Person.class");
        return file;
    }
    
    private byte[] getClassBytes(File file) throws Exception
    {
        // 这里要读入.class的字节,因此要使用字节流
        FileInputStream fis = new FileInputStream(file);
        FileChannel fc = fis.getChannel();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel wbc = Channels.newChannel(baos);
        ByteBuffer by = ByteBuffer.allocate(1024);
        
        while (true)
        {
            int i = fc.read(by);
            if (i == 0 || i == -1)
                break;
            by.flip();
            wbc.write(by);
            by.clear();
        }
        
        fis.close();
        
        return baos.toByteArray();
    }
}
package info.lumanman003;

public class Person
{
    private String name;
    
    public Person()
    {
        
    }
    
    public Person(String name)
    {
        this.name = name;
    }
    
    public String getName()
    {
        return name;
    }
    
    public void setName(String name)
    {
        this.name = name;
    }
    
    public String toString()
    {
    	System.out.println("被动态加载了");
        return "I am a person, my name is " + name;
    }
}
package info.lumanman003;

import java.lang.reflect.Method;

public class TestMyClassLoader
{
    public static void main(String[] args) throws Exception
    {
        MyClassLoader mcl = new MyClassLoader();        
        Class<?> findClass = mcl.findClass("info.lumanman003.Person");
        
        //Class<?> c1 = Class.forName("com.xrq.classloader.Person", true, mcl); 
        Object obj = findClass.newInstance();
        Method[] methods = findClass.getMethods();
        for(int i=0;i<methods.length;i++){
        	if(methods[i].getName().equals("toString")){
        		System.out.println("哈哈哈");
        		methods[i].invoke(obj);
        	}
        }
    }
}