【java高级程序设计】类加载(内部类预先装载vs外部、按需装载)||自食用||含疑问

30 篇文章 1 订阅
16 篇文章 0 订阅

目录

一、类 

二、类型信息的存储与装载 

1. 预先加载(类内部、类外部)

  2. 按需加载

 3. 自定义加载(存疑)​

 代码段:


一、类 

二、类型信息的存储与装载 

1. 预先加载(类内部、类外部)

 

  2. 按需加载

 3. 自定义加载(存疑)

 

 

 代码段:

package customLoader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/** 
 * @author: y9
 * @Date: 2021/09/26
 */

public class FileSystemClassLoader extends ClassLoader{
	private String rootDir;
	public FileSystemClassLoader(String rootDir) {
		this.rootDir = rootDir;
	}
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException{
		byte[] classData = getClassData(name);
		if(classData == null) {
			throw new ClassNotFoundException();
		}
		else {
			return defineClass(name, classData, 0, classData.length);
		}
	}
	private byte[] getClassData(String className) {
		String path = classNameToPath(className);
		System.out.println(path);
		try{
		InputStream ins = new FileInputStream(path);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int bufferSize = 4096;
		byte[] buffer = new byte[bufferSize];
		int bytesNumRead = 0;
		while((bytesNumRead = ins.read(buffer))!=-1) {
			baos.write(buffer,0,bytesNumRead);
		}
		return baos.toByteArray();
		}catch(IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	private String classNameToPath(String className) {
		//return rootDir + "\\" + className + ".class";
		return rootDir + File.separatorChar + className.replace('.',File.separatorChar) + ".class";
	}
}

package customLoader;  
 
/** 
 * @author: y9
 * @Date: 2021/09/26
 */

public class Sample {
	private Sample instance;
	public void setSample(Object newInstance) {
		this.instance = (Sample)newInstance;
	}
}

package customLoader;

import java.lang.reflect.Method;

/** 
 * @author: y9
 * @Date: 2021/09/26
 */

public class FileSystemClassLoaderTest {

	public static void main(String[] args) {
		new FileSystemClassLoaderTest().testClassIdentity();

	}

	private void testClassIdentity() {
		String classDataRootPath = "D:\\java\\Studentsa\\bin\\customLoader";
		FileSystemClassLoader fscl1 = new FileSystemClassLoader(classDataRootPath);
		FileSystemClassLoader fscl2 = new FileSystemClassLoader(classDataRootPath);
		String className = "Sample";
		try {
			Class<?> class1 = fscl1.loadClass(className);
			Object obj1 = class1.newInstance();
			Object obj3 = class1.newInstance();
			
			Class<?> class2 = fscl2.loadClass(className);
			Object obj2 = class2.newInstance();
			Method setSampleMethod = 
					class1.getMethod("setSample", java.lang.Object.class);
			setSampleMethod.invoke(obj1,obj3);
			
			setSampleMethod.invoke(obj1, obj2);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值