关于java的反射机置一段有用的代码:
package test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Test{
public String a1="b1", a2="b2",a3="b3",a4="b4",a5="b5",a6="b6";
private void methed1(){}
public void methed2(){}
public void methed3(){}
public void methed4(){}
public String methed5(){
return a1;
}
public static void main(String args []){
try {
Class clzz = Class.forName("test.Test");
Object obj = new Test();
if(obj instanceof Test){
System.out.println("OK");
}
System.out.println(obj.getClass().getSimpleName());
Method method;
try {
method = clzz.getMethod("methed5");
Object obj2 = method.invoke(obj);
System.out.println("obj2 = " + obj2);
} catch(Exception e){
e.printStackTrace();
}
System.out.println(Test.class);
System.out.println(Test.class.getClassLoader().getResourceAsStream("test.projectInfoBeanMethodMap.properties"));
System.out.println(Arrays.asList(Test.class.getFields()));
Field[] field = clzz.getDeclaredFields();
System.out.println(field.length);
System.out.println(field[1].getName());
System.out.println(Arrays.asList(field));
Method[] method2 = clzz.getDeclaredMethods();
System.out.println(method2.length);
System.out.println(Arrays.asList(method2));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
projectInfoBeanMethodMap.properties的内容:
ProjectInfoBean.GROUP_NUM=集团编号
ProjectInfoBean.COM_NUM=公司编号
ProjectInfoBean.USER_ID=员工编号