自定义ClassLoader

实现功能:从文件或 InputStream 中加载一个类到内存中,并可以正常调用该类的方法.

// org.future.StreamClassLoader.java
package org.future.loader;

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

/**
 *@date   2010-3-27 下午08:34:20
 *@author dycc
 *@file   org.future.StreamClassLoader.java
 */
public class StreamClassLoader extends ClassLoader{
	// 当前类的一个实例
	private static StreamClassLoader instance;
	/**
	 * 构造方法设为私有的
	 */
	private StreamClassLoader(){
	}
	/**
	 * 返回当前类的一个实例
	 * @return
	 */
	public static StreamClassLoader getInstance(){
		if(instance == null){
			instance = new StreamClassLoader();
		}
		return instance;
	}
	/**
	 * 从文件中加载一个类
	 * @param filePath   例: D:/temp/classes/AppleTree.class
	 * @param className  例: org.future.locale.AppleTree
	 * @return  The Class object that was created from the specified file.
	 */
	@SuppressWarnings(value="unchecked")
	public Class load(String filePath,String className)
		throws FileNotFoundException,IOException{
		FileInputStream in = new FileInputStream(filePath);
		return load(in,className);
	}
	
	/**
	 * 从流中加载一个类
	 * @param in
	 * @param className  例: org.future.locale.AppleTree
	 * @return  The Class object that was created from the specified inputStream.
	 */
	@SuppressWarnings(value="unchecked")
	public Class load(InputStream in,String className)throws IOException{
		// 如果该类已经被加载过则直接返回
		Class cls =  this.findLoadedClass(className);
		if(cls != null){
			return cls;
		}
		// 从流中读取所有的字节
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = -1;
		while((len = in.read(data, 0, data.length)) > 0){
			out.write(data, 0, len);
		}
		data = out.toByteArray();
		in.close();
		out.close();
		// 调用父类方法从字节数组中加载类
		return defineClass(className, data, 0, data.length);
	}
}

// 测试类:org.future.loader.StreamClassLoaderTest.java
package org.future.loader;
/**
 *@date   2010-3-27 下午08:53:25
 *@author dycc
 *@file   org.future.loader.StreamClassLoaderTest.java
 */
public class StreamClassLoaderTest {
	@SuppressWarnings(value="unchecked")
	public static void main(String[] args)throws Exception {
	    String filePath = "D:/eclipse/workspace/Test/build/classes/AppleTree.class";
	    String className = "org.future.locale.AppleTree";
	    StreamClassLoader loader = StreamClassLoader.getInstance();
	    Class cls = loader.load(filePath, className);
	    Tree tree = (Tree)cls.newInstance();
	    System.out.println("name=" + tree.getName());
	    System.out.println("height=" + tree.getHeight());
	    
	    cls = loader.load(filePath, className);
	    tree = (Tree)cls.newInstance();
	    System.out.println("name=" + tree.getName());
	    System.out.println("height=" + tree.getHeight());
    }
}
// 一个接口:org.future.loader.Tree.java
package org.future.loader;
/**
 *@date   2010-3-27 下午08:57:09
 *@author dycc
 *@file   org.future.loader.Tree.java
 */
public interface Tree {
	/**
	 * 名称
	 * @return
	 */
	public String getName();
	/**
	 * 高度
	 * @return
	 */
	public int getHeight();
}
// 一个实现类:org.future.locale.AppleTree.java
package org.future.locale;

import org.future.loader.Tree;

/**
 *@date   2010-3-27 下午08:59:42
 *@author dycc
 *@file   org.future.locale.AppleTree.java
 */
public class AppleTree implements Tree{
	// 获取名称
	public String getName() {
	    return "Apple Tree";
	}
	// 获取高度
	public int getHeight() {
	    return 3;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值