`

java 反射 获得属性值

    博客分类:
  • Java
 
阅读更多

通过java反射获得属性值

 

 

 

 

 

Java的反射非常强大,传递class, 可以动态的生成该类、取得这个类的所有信息,包括里面的属性、方法以及构造函数等,甚至可以取得其父类或父接口里面的内容。

obj.getClass().getDeclaredMethods();//取得obj类中自己定义的方法, 包括私有的方法。
obj.getClass().getMethods();//取得obj类中自己定义的方法及继承过来的方法, 但私有方法得不到。
同样, 对field也是一样,obj.getClass().getDeclaredFields();取得的是这个类中所有的属性,包括私有的field; 对obj.getClass().getFields();//取得是自己以及接继承来的属性, 但不能取得自己的私有属性。

Java代码 复制代码 收藏代码
  1. static Object create(Class clazz) throws Exception {
  2. Constructor con = clazz.getConstructor(String.class);
  3. Object obj = con.newInstance("test name");
  4. return obj;
  5. }
  6. static void invoke1(Object obj, String methodName)
  7. throws IllegalArgumentException, IllegalAccessException,
  8. InvocationTargetException, Exception, NoSuchMethodException {
  9. Method[] ms = obj.getClass().getDeclaredMethods();
  10. ms = obj.getClass().getMethods();
  11. for (Method m : ms) {
  12. // System.out.println(m.getName());
  13. if (methodName.equals(m.getName()))
  14. m.invoke(obj, null);
  15. }
  16. Method m = obj.getClass().getMethod(methodName, null);
  17. m.invoke(obj, null);
  18. }
  19. static void field(Class clazz) throws Exception {
  20. Field[] fs = clazz.getDeclaredFields();
  21. //fs = clazz.getFields();
  22. for (Field f : fs)
  23. System.out.println(f.getName());
  24. }
  25. static void annon(Class clazz) throws Exception {
  26. Annotation[] as = clazz.getAnnotations();
  27. }

      

例:

packagecom.juziku;

publicclassReflectPoint {
/**
* 私有变量x
*/
privateintx;

publicinty;
publicString str1 = "abc";
publicString str2 = "123";

publicReflectPoint(intx, inty) {
super();
this.x = x;
this.y = y;
}

}
packagecom.juziku;

importjava.lang.reflect.Field;

/**
* 反射例子
* @author sunlightcs
* 2011-3-5
* http://hi.juziku.com/sunlightcs/
*/
publicclassReflectTest {

publicstaticvoidmain(String[] args) throwsException{

ReflectPoint reflectPoint = newReflectPoint(3,5);

/**
* 通过反射,获得ReflectPoint属性的值
*
* reflectPoint.getClass()表示获得ReflectPoint的字节码,
* 当然也可以写成Field fieldY = ReflectPoint.class
* 或Class.forName("com.juziku.ReflectPoint");
* 获得某个类的字节码的方法有3种:
* ReflectPoint.class (类名.class)
* reflectPoint.getClass() (对象名.getClass())
* Class.forName("com.juziku.ReflectPoint") (Class.forName(类名的路径))
*
* reflectPoint.getClass().getField("y")表示获得ReflectPoint里y属性的位置
*/
Field fieldY = reflectPoint.getClass().getField("y");

/**
* fieldY.get(reflectPoint)表示获得reflectPoint对象里,y位置对应属性的值
*/
Object y = (Object)fieldY.get(reflectPoint);

/**
* 打印reflectPoint对象里,属于为y的值
*/
System.out.println(y);



/**
* 获得x属性的值,由于x属于为private类型,
* 所以要通过getDeclaredField方法查询x所在的位置
*/
Field fieldX = reflectPoint.getClass().getDeclaredField("x");

/**
* 由于x属于为private类型,也要设置成可访问,不然获取不到值
*/
fieldX.setAccessible(true);
Object x = (Object)fieldX.get(reflectPoint);
System.out.println(x);


/**
* 获得类型为String的所有值
*/
getStringValues(reflectPoint);


/**
* 修改str1属性的值
*/
onchageValue(reflectPoint);

}

privatestaticvoidgetStringValues(Object obj) throwsException{
/**
* 获得所有属性的位置
*/
Field[] fields = obj.getClass().getFields();

for(Field field : fields){
/**
* 这里建议用==,不建议用equals
* 如果field属性是String类型的话,那么跟String的字节码是同一份
*/
if(field.getType() == String.class){
String str = (String)field.get(obj);
System.out.println(str);
}
}
}

privatestaticvoidonchageValue(Object obj) throwsException{
Field field = obj.getClass().getField("str1");
System.out.println(field.get(obj));
field.set(obj, "22");
System.out.println(field.get(obj));
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics