Java Reflection Introspector

Source: Internet
Author: User

first, explain Introspector introspection, Self-examination.
In the Java.beans package in java, the original text description is:
 
   
  
  1. The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.
Chinese to the effect of
 
   
  
  1. Introspector提供了一种标准的方式作为工具来获取类的属性,时间,方法。
Typically used in reflection, view the internal information of a class. The following is a collection of reflection classes for the space-time change.
  
 
  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. }



Java Reflection Introspector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.