java基础之反射-通过反射调用某个属性的setter和getter方法

Java代码   收藏代码
  1. package com.gui.test.reflection;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import java.util.Hashtable;  
  5. import java.util.regex.Pattern;  
  6.   
  7. /** 
  8.  *  
  9.  * @desc 通过反射来动态调用get 和 set 方法 
  10.  * @date 2010-10-14 
  11.  * @Version 1.0 
  12.  */  
  13. public class SetMethodReflect {  
  14.   
  15.     private Class cls;  
  16.     /** 
  17.      * 传过来的对象 
  18.      */  
  19.     private Object obj;  
  20.   
  21.     /** 
  22.      * 存放get方法 
  23.      */  
  24.     private Hashtable<String, Method> getMethods = null;  
  25.     /** 
  26.      * 存放set方法 
  27.      */  
  28.     private Hashtable<String, Method> setMethods = null;  
  29.   
  30.     /** 
  31.      * 定义构造方法 -- 一般来说是个pojo 
  32.      *  
  33.      * @param o 目标对象 
  34.      */  
  35.     public SetMethodReflect(Object o) {  
  36.         obj = o;  
  37.         initMethods();  
  38.     }  
  39.   
  40.     /** 
  41.      *  
  42.      * @desc 初始化 
  43.      */  
  44.     public void initMethods() {  
  45.         getMethods = new Hashtable<String, Method>();  
  46.         setMethods = new Hashtable<String, Method>();  
  47.         cls = obj.getClass();  
  48.         Method[] methods = cls.getMethods();  
  49.         // 定义正则表达式,从方法中过滤出getter / setter 函数.  
  50.         String gs = "get(\\w+)";  
  51.         Pattern getM = Pattern.compile(gs);  
  52.         String ss = "set(\\w+)";  
  53.         Pattern setM = Pattern.compile(ss);  
  54.         // 把方法中的"set" 或者 "get" 去掉  
  55.         String rapl = "$1";  
  56.         String param;  
  57.         for (int i = 0; i < methods.length; ++i) {  
  58.             Method m = methods[i];  
  59.             String methodName = m.getName();  
  60.             if (Pattern.matches(gs, methodName)) {  
  61.                 param = getM.matcher(methodName).replaceAll(rapl).toLowerCase();  
  62.                 getMethods.put(param, m);  
  63.             } else if (Pattern.matches(ss, methodName)) {  
  64.                 param = setM.matcher(methodName).replaceAll(rapl).toLowerCase();  
  65.                 setMethods.put(param, m);  
  66.             } else {  
  67.                 // System.out.println(methodName + " 不是getter,setter方法!");  
  68.             }  
  69.         }  
  70.     }  
  71.   
  72.     /** 
  73.      *  
  74.      * @desc 调用set方法 
  75.      */  
  76.     public boolean setMethodValue(String property, boolean value) {  
  77.         Method m = setMethods.get(property.toLowerCase());  
  78.         if (m != null) {  
  79.             try {  
  80.                 // 调用目标类的setter函数  
  81.                 m.invoke(obj, value);  
  82.                 return true;  
  83.             } catch (Exception ex) {  
  84.                 System.out.println("invoke getter on " + property + " error: "  
  85.                         + ex.toString());  
  86.                 return false;  
  87.             }  
  88.         }  
  89.         return false;  
  90.     }  
  91.   
  92.     /** 
  93.      *  
  94.      * @desc 调用set方法 
  95.      */  
  96.     public boolean setMethodValue(String property, String value) {  
  97.         Method m = setMethods.get(property.toLowerCase());  
  98.         if (m != null) {  
  99.             try {  
  100.                 /** 
  101.                  * 调用obj类的setter函数 
  102.                  */  
  103.                 m.invoke(obj, value);  
  104.                 return true;  
  105.             } catch (Exception ex) {  
  106.                 System.out.println("invoke getter on " + property + " error: "  
  107.                         + ex.toString());  
  108.                 return false;  
  109.             }  
  110.         }  
  111.         return false;  
  112.     }  
  113.   
  114.     // 测试方法  
  115.     public static void main(String args[]) {  
  116.         Test ah = new Test();  
  117.         SetMethodReflect smr = new SetMethodReflect(ah);  
  118.         smr.setMethodValue("a"false);  
  119.         smr.setMethodValue("b"true);  
  120.         smr.setMethodValue("c"true);  
  121.   
  122.         System.out.println(ah.isA());  
  123.         System.out.println(ah.isB());  
  124.         System.out.println(ah.isC());  
  125.     }  
  126. }  
  127.   
  128.   
  129. // 一个model  
  130. class Test {  
  131.     boolean a;  
  132.     boolean b;  
  133.     boolean c;  
  134.     public boolean isA() {  
  135.         return a;  
  136.     }  
  137.     public void setA(boolean a) {  
  138.         this.a = a;  
  139.     }  
  140.     public boolean isB() {  
  141.         return b;  
  142.     }  
  143.     public void setB(boolean b) {  
  144.         this.b = b;  
  145.     }  
  146.     public boolean isC() {  
  147.         return c;  
  148.     }  
  149.     public void setC(boolean c) {  
  150.         this.c = c;  
  151.     }  
  152. }  

     

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值