1. import java.beans.BeanInfo;
  2. import java.beans.Introspector;
  3. import java.beans.PropertyDescriptor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. /**
  7. * 使用内省的方式操作JavaBean
  8. */
  9. public class IntroSpectorTest {
  10. public static void main(String[] args) throws Exception {
  11. ReflectPoint reflectPoint = new ReflectPoint(3,7);
  12. //调用JavaBean中方法的传统作法
  13. Class classz = reflectPoint.getClass();
  14. Field[] fields = classz.getDeclaredFields();
  15. for (Field field : fields) {
  16. String name = "set" + field.getName().toUpperCase();
  17. Method method = classz.getMethod(name, int.class);
  18. method.invoke(reflectPoint, 3);
  19. }
  20. System.out.println(reflectPoint);
  21. //使用内省的方式调用JavaBean的方法
  22. String propertyName = "x";
  23. //获得属性x的值
  24. Object retVal = getProperty2(reflectPoint, propertyName);
  25. System.out.println(retVal);
  26. //设置属性x的值
  27. setProperty(reflectPoint, propertyName,10);
  28. System.out.println(reflectPoint.getX());
  29. }
  30. /**
  31. * 设置属性的值
  32. * @param obj 属性所属的对象
  33. * @param propertyName 属性名
  34. * @param propertyValue 属性值
  35. */
  36. private static void setProperty(Object obj, String propertyName,Object propertyValue) throws Exception {
  37. PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,obj.getClass());
  38. Method setMethod = pd2.getWriteMethod();
  39. setMethod.invoke(obj, propertyValue);
  40. }
  41. /**
  42. * 获得对象的属性值
  43. * @param obj 属性所属的对象
  44. * @param propertyName 属性名
  45. * @return 属性的值
  46. */
  47. private static Object getProperty(Object obj, String propertyName) throws Exception {
  48. PropertyDescriptor pd = new PropertyDescriptor(propertyName,obj.getClass());
  49. Method getMethod = pd.getReadMethod();  //获得get方法
  50. Object retVal = getMethod.invoke(obj);
  51. return retVal;
  52. }
  53. /**
  54. * 使用内省操作javabean
  55. * @param obj 属性所属的对象
  56. * @param propertyName 属性名
  57. * @return 属性的值
  58. */
  59. private static Object getProperty2(Object obj, String propertyName) throws Exception {
  60. Object retVal = null;
  61. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  62. PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
  63. for (PropertyDescriptor pd : pds) {
  64. if (pd.getName().equals(propertyName)) {
  65. Method getMethod = pd.getReadMethod();
  66. retVal = getMethod.invoke(obj);
  67. break;
  68. }
  69. }
  70. return retVal;
  71. }
  72. }

使用内省的方式操作JavaBean的更多相关文章

  1. 内省机制(操作javaBean的信息)

    内省机制(操作javaBean的信息) ----是不是联想到了反射机制了哈,这两者有什么区别呢? 1.内省机制和反射机制的联系 ■ 其实内省机制也是通过反射来实现的,而反射是对一切类都适合去动态获取类 ...

  2. 使用内省方式操作JavaBean

    内省,英文中称作introspector.主要对javaBean进行操作,JavaBean是一个特殊的Java类,该类中方法名符合特定的规则(其实就是getXXX,setXXX),我们一般是利用get ...

  3. java高新技术-操作javaBean

    1. 对javaBean的简单内省操作 public class IntroSpectorTest { public static void main(String[] args) throws Ex ...

  4. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...

  5. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)(转)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...

  6. Android-Sqlite-OOP方式操作增删改查

    之前写的数据库增删改查,是使用SQL语句来实现的,Google 就为Android开发人员考虑,就算不会SQL语句也能实现增删改查,所以就有了OOP面向对象的增删改查方式 其实这种OOP面向对象的增删 ...

  7. Oracle.DataAccess.dll方式操作oracle数据库

    Oracle.DataAccess.dll方式操作oracle数据库 一.查询语句: using (OracleConnection conn = new OracleConnection(Syste ...

  8. TX2 用文件IO的方式操作GPIO

    概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户空间,GPIO 的 ...

  9. Win10 64位系统ADO方式操作数据库失败解决方法

    VC操作Access数据库一般通过ODBC.ADO.DAO等方式,但在我的Win10 64位操作系统中,通过ADO方式操作数据库会失败,无法读取数据.解决方法:1.首先确保Win10操作系统ado目录 ...

随机推荐

  1. C语言串口

    可以用open和fopen来打开文件,open偏底层,fopen来自于open更顶层.(根据公司某个项目看了源码用的open) #include <stdio.h>#include < ...

  2. 高通LCD驱动调试

    本文转载自:http://www.itgo.me/a/x6305658852004979994/lcd%20qcom 来自 :http://blog.csdn.net/dacaozuo/article ...

  3. Oracle数据库类型总结

    RACLE基本数据类型(亦叫内置数据类型 built-in datatypes)可以按类型分为:字符串类型.数字类型.日期类型.LOB类型.LONG RAW& RAW类型.ROWID & ...

  4. EF Code-First 学习之旅 Code First Conventions

    协定是一系列的默认规则用来自动配置领域中的概念模型 1:类型发现 Code-First对包含DBSet属性的类型创建表(包括这些类型的所有引用类型) public class Student { pu ...

  5. linux基础(2)-网卡配置

    常用网卡配置参数 DEVICE=eth0     #指出设备名称 HWADDR=00:0C:29:3C:D2:CA     #网卡的mac地址TYPE=Ethernet     #网络类型为Ether ...

  6. 由于ptrace.h文件导致的内核编译出错的解决方法

    arch/x86/kernel/ptrace.c:1472:17: error: conflicting types for 'syscall_trace_enter'  In file includ ...

  7. PostgreSQL的日志文件介绍

    PostgreSQL的日志文件 pg_log:数据库活动日志(也就是数据库的操作日志): pg_xlog:事务日志: pg_clog:事务状态日志(pg_clog是pg_xlog的辅助日志). 现在主 ...

  8. img标签显示本地文件

    html: <img src="__IMG__/male.png" id="imgfpic1" style="height: 100%; wid ...

  9. jsonp: js跨域

    JSONP是JSON with padding(填充式JSON或参数式JSON)的简写,是应用JSON的一种新方法,常用于服务器与客户端跨源通信,在后来的Web服务中非常流行.本文将详细介绍JSONP ...

  10. WPF绑定数据源之RelativeSource

    Command="{Binding ConfirmRegisterCommand}" CommandParameter="{Binding RelativeSource= ...