日期:2014-05-20 浏览次数:20845 次
package com.uestc.classload;
import java.io.File;
import java.io.InputStream;
public class TestClassIdentity {
static String classDataRootPath = "E:\\MyEclipse 10\\OOM\\bin";
public static String classNameToPath(String className) {
return classDataRootPath + File.separatorChar
+ className.replace('.', File.separatorChar) + ".class";
}
public static void main(String[] args) throws Exception{
ClassLoader classLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name)
throws ClassNotFoundException {
try {
InputStream is = getClass().getResourceAsStream(classNameToPath(name));
if(is == null){
return super.loadClass(name);
}else{
byte[] b = new byte[is.available()];
is.read(b);
return defineClass(name, b, 0, b.length);
}
} catch (Exception e) {
// TODO: handle exception
}
return super.loadClass(name);
}
};
Object object = classLoader.loadClass("com.uestc.classload.TestClassIdentity").newInstance();
System.out.println(object.getClass());
System.out.println(object.getClass().getClassLoader());
System.out.println(object instanceof com.uestc.classload.TestClassIdentity);
}
}