类加载器

来源:互联网 发布:淘宝商家直播入口 编辑:程序博客网 时间:2024/05/20 10:11


一、序言

  

顾名思义,类加载器(class loader)用来加载 Java 类到 Java 虚拟机中。一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源程序(.java 文件)在经过 Java 编译器编译之后就被转换成 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码,并转换成 java.lang.Class类的一个实例。每个这样的实例用来表示一个 Java 类。通过此实例的 newInstance()方法就可以创建出该类的一个对象。实际的情况可能更加复杂,比如 Java 字节代码可能是通过工具动态生成的,也可能是通过网络下载的。



代码如下,具体方法也在代码中:

       package com.zking.test;

import javax.xml.registry.infomodel.PersonName;

import org.junit.Test;

import com.veryedu.entity.ClassLoaderDIY;

public class TestClassLoader {
    
    @Test
    public void test3() throws InstantiationException, IllegalAccessException{
        //使用自己的类加载器 加载对象
        try {
            Class clazz=Class.forName("com.zking.entity.Person", true, new ClassLoaderDIY());
            System.out.println(clazz.newInstance());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    
    
    
    
    
    @Test
    public void test2(){
        //通过类加载器获取对象
        try {
            
            //获取类对象
            Class clazz=Class.forName("com.zking.entity.Person");
            
            //获取类加载器
            ClassLoader classLoader=clazz.getClassLoader();
            ClassLoader classLoaderParent=classLoader.getParent();
            ClassLoader classLoaderGrandParent=classLoaderParent.getParent();
            
            //查询该类加载器的类型
            System.out.println(classLoader);
            System.out.println(classLoaderParent);
            System.out.println(classLoaderGrandParent);
            
//            Person person=(Person) clazz.newInstance();
//            person.setPname("李四");
//            System.out.println(person.getPname());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void test1(){
        //之前
//        Person person=new Person();
//        person.setPname("张三");
//        System.out.println(person.getPname());
    }
}


下面为自定义类加载器


package com.veryedu.entity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * 自定义类加载器
 * @author Administrator
 *
 */
public class ClassLoaderDIY extends ClassLoader{

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        System.out.println("自定义类加载器");
        System.out.println(name);
        //所有的.替换成\
        name=name.replaceAll("\\.", "/");
        System.out.println("替换后:"+name);
        //根据name找到桌面上 相对应的 person.class文件
        String desktopPath="C:\\Users\\Administrator\\Desktop\\"+name+".class";
        System.out.println(desktopPath);
        try {
            FileInputStream fis=new FileInputStream(desktopPath);
            System.out.println(fis.available());
            int len=0;
            byte[] b=new byte[fis.available()];
            len=fis.read(b);
            return defineClass(null,b,0,len);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
}

上面的测试类,有加载自定义类加载器的方法,调用Class对象的forName("对象全限定名",Boolean类型(true),实例化自己写的类加载器(new  new ClassLoaderDIY())),就哦了,其他就可以进行赋值,调用了。