实现功能:从文件或 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;
}
}