2013年(92)
分类: 信息化
2013-05-17 23:20:45
2.?[文件] ClassLoaderTest.java?~?3KB???? 下载(4)???? 跳至 [1] [2] [3] [4] [全屏预览] package classloader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
*
* 一、ClassLoader加载类的次第 1.调用 findLoadedClass(String) 来检查是不是现已加载类。 2.在父类加载器上调用
* loadClass 方法。如果父类加载器为 null,则运用虚拟机的内置类加载器。 3.调用 findClass(String) 方法查找类。
* 二、完结自己的类加载器 1.获取类的class文件的字节数组 2.将字节数组转换为Class类的实例
*
*
* @author lei 2011-9-1
*/
public class ClassLoaderTest {
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
MyClassLoader cl = new MyClassLoader("myClassLoader");
// cl.setPath("E:\\workspace\\test\\bin\\");
cl.setPath("E:\\workspace_hadoop\\htest\\bin\\");
// cl.setPath("E:\\");
Class demo = cl.loadClass("classloader.hAnimal");
//卸载
cl=null;
System.gc();
cl = new MyClassLoader("myClassLoader");
cl.setPath("E:\\workspace_hadoop\\htest\\bin\\");
demo = cl.loadClass("classloader.hAnimal");
System.out.println("============== =======================");
Field[] field = demo.getDeclaredFields();
for (int i = 0; i < field.length; i ) {
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
Class type = field[i].getType();
System.out.println(priv " " type.getName() " "
field[i].getName() ";");
}
System.out.println("===============完结的接口或许父类的特色========================");
// 取得完结的接口或许父类的特色
Field[] filed1 = demo.getFields();
for (int j = 0; j < filed1.length; j ) {
// 权限修饰符
int mo = filed1[j].getModifiers();
String priv = Modifier.toString(mo);
// 特色类型
Class type = filed1[j].getType();
System.out.println(priv " " type.getName() " "
filed1[j].getName() ";");
}
// //得到类的实例
classloadtest animal= (classloadtest)demo.newInstance();
animal.test();
try{
Method method=demo.getMethod("test");
method.invoke(demo.newInstance());
// method=demo.getMethod("sayHello", String.class,int.class);
// method.invoke(demo.newInstance(),"Rollen",20);
}catch (Exception e) {
e.printStackTrace();
}
}
}