一、解释
Introspector  内省,自我检查。
位于java中的java.beans包中,其原文说明文为:
  1. The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.
中文大意为
  1. Introspector提供了一种标准的方式作为工具来获取类的属性,时间,方法。
通常用在反射中,查看类的内部信息。
以下为收集到的一个,空间换时间的反射类。
  1. // 类属性缓存,空间换时间
  2. private static final ConcurrentMap, PropertyDescriptor[]> classPropCache =
  3. new ConcurrentHashMap, PropertyDescriptor[]>(64);
  4. /**
  5. * 获取Bean的属性
  6. * @param bean
  7. * @return
  8. */
  9. private static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
  10. Class beanClass = bean.getClass();
  11. PropertyDescriptor[] cachePds = classPropCache.get(beanClass);
  12. if (null != cachePds) {
  13. return cachePds;
  14. }
  15. try {
  16. BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
  17. cachePds = beanInfo.getPropertyDescriptors();
  18. classPropCache.put(beanClass, cachePds);
  19. return cachePds;
  20. } catch (IntrospectionException e) {
  21. throw new RuntimeException(e);
  22. }
  23. }
  24. /**
  25. * 获取Bean的属性
  26. * @param bean bean
  27. * @param propertyName 属性名
  28. * @return 属性值
  29. */
  30. public static Object getProperty(Object bean, String propertyName) {
  31. PropertyDescriptor[] beanPds = getPropertyDescriptors(bean);
  32. for (PropertyDescriptor propertyDescriptor : beanPds) {
  33. if (propertyDescriptor.getName().equals(propertyName)){
  34. Method readMethod = propertyDescriptor.getReadMethod();
  35. if (null == readMethod) {
  36. continue;
  37. }
  38. if (!readMethod.isAccessible()) {
  39. readMethod.setAccessible(true);
  40. }
  41. try {
  42. return readMethod.invoke(bean);
  43. } catch (Throwable ex) {
  44. throw new RuntimeException("Could not read property '" + propertyName + "' from bean", ex);
  45. }
  46. }
  47. }
  48. return null;
  49. }
  50. /**
  51. * 设置Bean属性
  52. * @param bean bean
  53. * @param propertyName 属性名
  54. * @param value 属性值
  55. */
  56. public static void setProperty(Object bean, String propertyName, Object value) {
  57. PropertyDescriptor[] beanPds = getPropertyDescriptors(bean);
  58. for (PropertyDescriptor propertyDescriptor : beanPds) {
  59. if (propertyDescriptor.getName().equals(propertyName)){
  60. Method writeMethod = propertyDescriptor.getWriteMethod();
  61. if (null == writeMethod) {
  62. continue;
  63. }
  64. if (!writeMethod.isAccessible()) {
  65. writeMethod.setAccessible(true);
  66. }
  67. try {
  68. writeMethod.invoke(bean, value);
  69. } catch (Throwable ex) {
  70. throw new RuntimeException("Could not set property '" + propertyName + "' to bean", ex);
  71. }
  72. }
  73. }
  74. }


最新文章

  1. linux fdisk命令使用
  2. CSS实战中经常出现的问题。
  3. static 修饰内部类
  4. Windows 内存架构
  5. JS判断用户是否在线的方法
  6. [GC]一个简单的Garbage Collector的实现
  7. activemq spring 配置
  8. DHTMLX地图开发参考示例摘录
  9. js在web绘制在页上的圆
  10. HTML5网页制作好好玩啊
  11. 微信H5支付 遇到坑的一些解决方法
  12. cdh 安装系列2--cdh manager product 安装
  13. Android硬件入门-照相机
  14. docker tag 详解
  15. ajax响应报文可以被浏览器缓存的必要条件
  16. hasura graphql 角色访问控制
  17. C# 解压gzip文件(.tgz)
  18. 【Java】使用CSVUtils生成文件并供下载
  19. 北京Uber优步司机奖励政策(3月23日)
  20. ACE线程管理机制-并发控制(2)

热门文章

  1. 【Python】微博自动抢红包
  2. 如何生动形象、切中要点地讲解 OSI 七层模型和两主机传输过程
  3. Web通信
  4. java中定义enum示例
  5. css按钮口诀 - CSS BUG顺口溜
  6. JS 对应CSS 样式
  7. [转]MySQL-5.7 Update语句详解
  8. 解密SVM系列(二):SVM的理论基础
  9. Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io .nativeio.NativeIO$Windows.createDirectoryWithMode0(Ljava/lang/String;I)V
  10. 微信怎样做SEO