自定义classloader实现

package classloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 文件加载类 可根据MyFileClassLoader 从文件中动态生成类
 * 
 */
public class MyFileClassLoader extends ClassLoader {

	private String classPath;

	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		MyFileClassLoader fileClsLoader = new MyFileClassLoader();
		fileClsLoader.setClassPath("c:\\003");
		Class cls = fileClsLoader
				.loadClass("classloader.TestCls");
		Object obj = cls.newInstance();
		Method[] mthds = cls.getMethods();
		for (int i = 0; i < mthds.length; i++) {
			Method mthd = mthds[i];
			String methodName = mthd.getName();
			System.out.println("mthd.name=" + methodName);
		}
		System.out.println("obj.class=" + obj.getClass().getName());
		System.out.println("obj.class=" + cls.getClassLoader().toString());
		System.out.println("obj.class="
				+ cls.getClassLoader().getParent().toString());
	}

	/**
	 * 根据类名字符串从指定的目录查找类,并返回类对象
	 */
	protected Class findClass(String name) throws ClassNotFoundException {
		byte[] classData = null;
		try {
			classData = loadClassData(name);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.defineClass(name, classData, 0, classData.length);
	}

	/**
	 * 根据类名字符串加载类 byte 数据流
	 * 
	 * @param name
	 *            类名字符串 例如: com.cmw.entity.SysEntity
	 * @return 返回类文件 byte 流数据
	 * @throws IOException
	 */
	private byte[] loadClassData(String name) throws IOException {
		File file = getFile(name);
		FileInputStream fis = new FileInputStream(file);
		byte[] arrData = new byte[(int) file.length()];
		fis.read(arrData);
		return arrData;
	}

	/**
	 * 根据类名字符串返回一个 File 对象
	 * 
	 * @param name
	 *            类名字符串
	 * @return File 对象
	 * @throws FileNotFoundException
	 */
	private File getFile(String name) throws FileNotFoundException {
		File dir = new File(classPath);
		if (!dir.exists())
			throw new FileNotFoundException(classPath + " 目录不存在!");
		String _classPath = classPath.replaceAll("[\\\\]", "/");
		int offset = _classPath.lastIndexOf("/");
		name = name.replaceAll("[.]", "/");
		if (offset != -1 && offset < _classPath.length() - 1) {
			_classPath += "/";
		}
		_classPath += name + ".class";
		dir = new File(_classPath);
		if (!dir.exists())
			throw new FileNotFoundException(dir + " 不存在!");
		return dir;
	}

	public String getClassPath() {
		return classPath;
	}

	public void setClassPath(String classPath) {
		this.classPath = classPath;
	}

}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值