使用反射获取和设置对象的值

使用Java如何获取对象的私有成员变量的值呢?

有一个bean:

Java代码   收藏代码
  1. public class Student2 {  
  2.     /*** 
  3.      * 学号 
  4.      */  
  5.     private String schoolNumber;  
  6.     private String classroom;  
  7.   
  8.       
  9.   
  10.     public void setSchoolNumber(String schoolNumber) {  
  11.         this.schoolNumber = schoolNumber;  
  12.     }  
  13.   
  14.     public String getClassroom() {  
  15.         return classroom;  
  16.     }  
  17.   
  18.     public void setClassroom(String classroom) {  
  19.         this.classroom = classroom;  
  20.     }  
  21.   
  22. }  

 测试:

Java代码   收藏代码
  1. @Test  
  2.     public void test_getObjectValue() throws SecurityException,  
  3.             IllegalArgumentException, NoSuchFieldException,  
  4.             IllegalAccessException {  
  5.         Student2 p = new Student2();  
  6.         p.setPersonName("哥斯拉");  
  7.         System.out.println(ReflectHWUtils.getObjectValue(p, "personName"));  
  8.     }  

运行结果:哥斯拉 

 

getObjectValue的实现:

Java代码   收藏代码
  1. /*** 
  2.      * 获取指定对象的属性值 
  3.      *  
  4.      * @param obj 
  5.      * @param name 
  6.      *            :Field 
  7.      * @return 
  8.      * @throws SecurityException 
  9.      * @throws NoSuchFieldException 
  10.      * @throws IllegalArgumentException 
  11.      * @throws IllegalAccessException 
  12.      */  
  13.     public static Object getObjectValue(Object obj, Field name)  
  14.             throws SecurityException, NoSuchFieldException,  
  15.             IllegalArgumentException, IllegalAccessException {  
  16.   
  17.         // Field f = getSpecifiedField(obj.getClass(), name.getName());  
  18.         if (name == null) {  
  19.             System.out.println("[ReflectHWUtils.getObjectValue]"  
  20.                     + obj.getClass().getName() + " does not has field " + name);  
  21.             return null;  
  22.         }  
  23.         name.setAccessible(true);  
  24.         return name.get(obj);  
  25.     }  
  26.   
  27.     /*** 
  28.      * 获取指定对象的属性值 
  29.      *  
  30.      * @param obj 
  31.      * @param propertyName 
  32.      * @return 
  33.      * @throws SecurityException 
  34.      * @throws NoSuchFieldException 
  35.      * @throws IllegalArgumentException 
  36.      * @throws IllegalAccessException 
  37.      */  
  38.     public static Object getObjectValue(Object obj, String propertyName)  
  39.             throws SecurityException, NoSuchFieldException,  
  40.             IllegalArgumentException, IllegalAccessException {  
  41.         if (ValueWidget.isNullOrEmpty(propertyName)) {  
  42.             return null;  
  43.         }  
  44.         Class<?> clazz = obj.getClass();  
  45.         Field name = getSpecifiedField(clazz, propertyName);  
  46.         if (ValueWidget.isNullOrEmpty(name)) {  
  47.             propertyName=ValueWidget.title(propertyName);//convert "Key2" to "key2"  
  48.             name = getSpecifiedField(clazz, propertyName);  
  49.               
  50.             if (ValueWidget.isNullOrEmpty(name)) {  
  51.                 System.out.println("[ReflectHWUtils.getObjectValue]"  
  52.                         + obj.getClass().getName() + " does not has field "  
  53.                         + propertyName);  
  54.             return null;  
  55.             }  
  56.         }  
  57.         return getObjectValue(obj, name);  
  58.     }  

 

 

 

 

使用反射设置对象的值

 

Java代码   收藏代码
  1. @Test  
  2.     public void test_setObjectValue() throws SecurityException,  
  3.             IllegalArgumentException, NoSuchFieldException,  
  4.             IllegalAccessException {  
  5.         Student2 p = new Student2();  
  6.         ReflectHWUtils.setObjectValue(p, "classroom""三六班");  
  7.         System.out.println(p.getClassroom());  
  8.     }  

 运行结果:

 

三六班

setObjectValue 的实现:

 

Java代码   收藏代码
  1. /*** 
  2.      * 设置对象的属性值。 
  3.      *  
  4.      * @param obj 
  5.      * @param propertyName 
  6.      *            : property name 
  7.      * @param propertyValue 
  8.      *            : value of property<br> must be String or Field 
  9.      * @throws SecurityException 
  10.      * @throws NoSuchFieldException 
  11.      * @throws IllegalArgumentException 
  12.      * @throws IllegalAccessException 
  13.      */  
  14.     public static void setObjectValue(Object obj, Object propertyName,  
  15.             Object propertyValue) throws SecurityException,  
  16.             NoSuchFieldException, IllegalArgumentException,  
  17.             IllegalAccessException {  
  18.         if (ValueWidget.isNullOrEmpty(propertyName)  
  19.                 || ValueWidget.isNullOrEmpty (propertyValue)) {  
  20.             return;  
  21.         }  
  22.         Class<?> clazz = obj.getClass();  
  23.         Field name = null;  
  24.         if(propertyName instanceof String){  
  25.         name=getSpecifiedField(clazz, (String)propertyName);  
  26.         }else{  
  27.             name=(Field)propertyName;  
  28.         }  
  29.         name.setAccessible(true);  
  30.         name.set(obj, propertyValue);  
  31.   
  32.     }  

 测试类:io0007-find_progess\src\test\java\com\test\UnitTest.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值