[原创]内省原理的模拟_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
3
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3206 | 回复: 2   主题: [原创]内省原理的模拟        下一篇 
鲲鹏展翅
注册用户
等级:少校
经验:1148
发帖:79
精华:9
注册:2012-11-19
状态:离线
发送短消息息给鲲鹏展翅 加好友    发送短消息息给鲲鹏展翅 发消息
发表于: IP:您无权察看 2012-11-22 9:02:52 | [全部帖] [楼主帖] 楼主

1.编写MyPropertyDescriptor,即模拟内省类

 package com.ykp.property;
import java.beans.IntrospectionException;
import java.lang.reflect.Method;
/**
 * 模拟内省的工作原理
 * 
 * @author燕鲲鹏
 * @version 1.0 2012-8-22

 */
publicclass MyPropertyDescriptor {
      /**
 * 演示内省的工作原理,即如何设置属性的值的 1.传入一个属性名name以及javabean的字节码获得一个PropertyDescriptor对象
* 2.用该对象通过内省里边的一个getWriteMethod获得setName方法 3.用获得的方法通过反射执行该方法,从而设置属性的值
 */
      // 属性名
      private String propertyName;
      // javabean的类字节码
      private Class<?>beanClass;
      /**
 * 构造方法
 * 

 * @param string

 * @param class1

 */
      publicMyPropertyDescriptor(String propertyName, Class<?>beanClass)
      throwsIntrospectionException {
            this.propertyName = propertyName;
            this.beanClass = beanClass;
      }
      /**
 * 通过getWriteMethod方法获得method对象
 */
      public Method getWriteMethod() throws Exception {
            // 获得属性的首字母并且转换成大写
            charfirstChara = this.propertyName.toUpperCase().charAt(0);
            System.err.println("firstChara-->" + firstChara);
            // 获得属性的非首字母
            String end = this.propertyName.substring(1);
            // 通过字符串拼接组成setXxx方法名
            String methodName = "set" + firstChara + end;
            System.err.println("methodName-->" + methodName);
            // 通过函数名利用反射获得该属性的setXxx方法对象
            Method m = this.beanClass.getMethod(methodName,
            this.propertyName.getClass());
            return m;
      }
      /**
 * 通过getWriteMethod方法获得method对象
 */
      public Method getReadMethod() throws Exception {
            // 获得属性的首字母并且转换成大写
            charfirstChara = this.propertyName.toUpperCase().charAt(0);
            System.err.println("firstChara-->" + firstChara);
            // 获得属性的非首字母
            String end = this.propertyName.substring(1);
            // 通过字符串拼接组成setXxx方法名
            String methodName = "get" + firstChara + end;
            System.err.println("methodName-->" + methodName);
            // 通过函数名利用反射获得该属性的setXxx方法对象
            Method m = this.beanClass.getMethod(methodName);
            return m;
      }
      /**
 * 测试
 */
      publicstaticvoid main(String[] args) {
            String name = "name";
            charfirstChara = name.toUpperCase().charAt(0);
            System.err.println("firstChara-->" + firstChara);
            String end = name.substring(1);
            String methodName = "set" + firstChara + end;
            System.err.println("methodName-->" + methodName);
            try {
                  MyPropertyDescriptormpd = newMyPropertyDescriptor("name",
                  User.class);
                  Method m = mpd.getWriteMethod();
                  System.err.println("m-->" + m);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}


2.用模拟的内省类来设置属性值

 packagecom.ykp.property;
importjava.lang.reflect.Method;
importorg.junit.Test;
/**
 * 使用模拟的内省类进行属性的设置和获得
 * 
 * @author燕鲲鹏
 * @version 1.0 2012-8-22

 */
publicclassTestMyPropertyDescriptor {
      /**
 * 通过模拟的内省类来设置user内的属性值
 */
      @Test
      publicvoidsetProperty() {
            try {
                  // 定义一个User对象
                  User user = newUser();
                  System.err.println("设置之前的user-->" + user);
                  // 获得一个MyPropertyDescriptor
                  MyPropertyDescriptorpd_id = newMyPropertyDescriptor("id", User.class);
                  // 获得指定属性name在其类里边的set方法
                  Method m_id = pd_id.getWriteMethod();
                  System.err.println("m_id-->"+m_id);
                  // 通过反射来设置属性name的值
                  m_id.invoke(user, "1");
                  // 获得一个MyPropertyDescriptor
                  MyPropertyDescriptorpd_name = newMyPropertyDescriptor("name", User.class);
                  // 获得指定属性name在其类里边的set方法
                  Method m_name = pd_name.getWriteMethod();
                  System.err.println("m_name-->"+m_name);
                  // 通过反射来设置属性name的值
                  m_name.invoke(user, "燕鲲鹏");
                  // 获得一个MyPropertyDescriptor
                  MyPropertyDescriptorpd_pwd = newMyPropertyDescriptor("pwd", user.getClass());
                  // 获得指定属性pwd在其类里边的set方法
                  Method m_pwd = pd_pwd.getWriteMethod();
                  System.err.println("m_pwd-->"+m_pwd);
                  // 通过反射来设置属性pwd的值
                  m_pwd.invoke(user, "ykp");
                  // 输出uesr对象,测试是否设置成功
                  System.err.println("设置之后的user-->" + user);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      /**
 * 通过模拟的内省类来获得user内的属性值
 */
      @Test
      publicvoidgetProperty() {
            try {
                  // 定义一个User对象
                  User user = newUser();
                  //设置user对象的属性值
                  user.setId("2");
                  user.setName("燕鲲鹏");
                  user.setPwd("zss");
                  System.err.println("设置的user-->" + user);
                  // 获得一个MyPropertyDescriptor
                  MyPropertyDescriptorpd_id = newMyPropertyDescriptor("id", User.class);
                  // 获得指定属性name在其类里边的get方法
                  Method m_id = pd_id.getReadMethod();
                  System.err.println("m_id-->"+m_id);
                  // 通过反射来获得属性name的值
                  String id= (String) m_id.invoke(user);
                  // 获得一个MyPropertyDescriptor
                  MyPropertyDescriptorpd_name = newMyPropertyDescriptor("name", User.class);
                  // 获得指定属性name在其类里边的get方法
                  Method m_name = pd_name.getReadMethod();
                  System.err.println("m_name-->"+m_name);
                  // 通过反射来获得属性name的值
                  String name = (String) m_name.invoke(user);
                  // 获得一个MyPropertyDescriptor
                  MyPropertyDescriptorpd_pwd = newMyPropertyDescriptor("pwd", user.getClass());
                  // 获得指定属性pwd在其类里边的get方法
                  Method m_pwd = pd_pwd.getReadMethod();
                  System.err.println("m_pwd-->"+m_pwd);
                  // 通过反射来获得属性pwd的值
                  String pwd = (String) m_pwd.invoke(user);
                  // 输出uesr对象,测试是否获得成功
                  System.err.println("获得的user-->" + "id=="+id+",name=="+name+",pwd=="+pwd);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}




赞(0)    操作        顶端 
zhou
注册用户
等级:中校
经验:2210
发帖:125
精华:6
注册:2012-11-19
状态:离线
发送短消息息给zhou 加好友    发送短消息息给zhou 发消息
发表于: IP:您无权察看 2012-11-22 14:22:31 | [全部帖] [楼主帖] 2  楼

北京联动北方科技有限公司

北京联动北方科技有限公司

北京联动北方科技有限公司

北京联动北方科技有限公司

测试



赞(0)    操作        顶端 
zhou
注册用户
等级:中校
经验:2210
发帖:125
精华:6
注册:2012-11-19
状态:离线
发送短消息息给zhou 加好友    发送短消息息给zhou 发消息
发表于: IP:您无权察看 2012-11-22 14:23:47 | [全部帖] [楼主帖] 3  楼

原创]内省原理的模拟



赞(0)    操作        顶端 
总帖数
3
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论