You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanItem.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.beans.BeanInfo;
  6. import java.beans.Introspector;
  7. import java.beans.PropertyDescriptor;
  8. import java.lang.reflect.Method;
  9. import java.util.Arrays;
  10. import java.util.Collection;
  11. import java.util.LinkedHashMap;
  12. import com.vaadin.data.Property;
  13. /**
  14. * A wrapper class for adding the Item interface to any Java Bean.
  15. *
  16. * @author IT Mill Ltd.
  17. * @version
  18. * @VERSION@
  19. * @since 3.0
  20. */
  21. @SuppressWarnings("serial")
  22. public class BeanItem extends PropertysetItem {
  23. /**
  24. * The bean which this Item is based on.
  25. */
  26. private final Object bean;
  27. /**
  28. * <p>
  29. * Creates a new instance of <code>BeanItem</code> and adds all properties
  30. * of a Java Bean to it. The properties are identified by their respective
  31. * bean names.
  32. * </p>
  33. *
  34. * <p>
  35. * Note : This version only supports introspectable bean properties and
  36. * their getter and setter methods. Stand-alone <code>is</code> and
  37. * <code>are</code> methods are not supported.
  38. * </p>
  39. *
  40. * @param bean
  41. * the Java Bean to copy properties from.
  42. *
  43. */
  44. public BeanItem(Object bean) {
  45. this(bean, getPropertyDescriptors(bean.getClass()));
  46. }
  47. /**
  48. * <p>
  49. * Creates a new instance of <code>BeanItem</code> using a pre-computed set
  50. * of properties. The properties are identified by their respective bean
  51. * names.
  52. * </p>
  53. *
  54. * @param bean
  55. * the Java Bean to copy properties from.
  56. * @param propertyDescriptors
  57. * pre-computed property descriptors
  58. */
  59. BeanItem(Object bean,
  60. LinkedHashMap<String, PropertyDescriptor> propertyDescriptors) {
  61. this.bean = bean;
  62. for (PropertyDescriptor pd : propertyDescriptors.values()) {
  63. final Method getMethod = pd.getReadMethod();
  64. final Method setMethod = pd.getWriteMethod();
  65. final Class<?> type = pd.getPropertyType();
  66. final String name = pd.getName();
  67. final Property p = new MethodProperty(type, bean, getMethod,
  68. setMethod);
  69. addItemProperty(name, p);
  70. }
  71. }
  72. /**
  73. * <p>
  74. * Creates a new instance of <code>BeanItem</code> and adds all listed
  75. * properties of a Java Bean to it - in specified order. The properties are
  76. * identified by their respective bean names.
  77. * </p>
  78. *
  79. * <p>
  80. * Note : This version only supports introspectable bean properties and
  81. * their getter and setter methods. Stand-alone <code>is</code> and
  82. * <code>are</code> methods are not supported.
  83. * </p>
  84. *
  85. * @param bean
  86. * the Java Bean to copy properties from.
  87. * @param propertyIds
  88. * id of the property.
  89. */
  90. public BeanItem(Object bean, Collection<?> propertyIds) {
  91. this.bean = bean;
  92. // Create bean information
  93. LinkedHashMap<String, PropertyDescriptor> pds = getPropertyDescriptors(bean
  94. .getClass());
  95. // Add all the bean properties as MethodProperties to this Item
  96. for (Object id : propertyIds) {
  97. PropertyDescriptor pd = pds.get(id);
  98. if (pd != null) {
  99. final String name = pd.getName();
  100. final Method getMethod = pd.getReadMethod();
  101. final Method setMethod = pd.getWriteMethod();
  102. final Class<?> type = pd.getPropertyType();
  103. final Property p = new MethodProperty(type, bean, getMethod,
  104. setMethod);
  105. addItemProperty(name, p);
  106. }
  107. }
  108. }
  109. /**
  110. * <p>
  111. * Creates a new instance of <code>BeanItem</code> and adds all listed
  112. * properties of a Java Bean to it - in specified order. The properties are
  113. * identified by their respective bean names.
  114. * </p>
  115. *
  116. * <p>
  117. * Note : This version only supports introspectable bean properties and
  118. * their getter and setter methods. Stand-alone <code>is</code> and
  119. * <code>are</code> methods are not supported.
  120. * </p>
  121. *
  122. * @param bean
  123. * the Java Bean to copy properties from.
  124. * @param propertyIds
  125. * ids of the properties.
  126. */
  127. public BeanItem(Object bean, String[] propertyIds) {
  128. this(bean, Arrays.asList(propertyIds));
  129. }
  130. /**
  131. * <p>
  132. * Perform introspection on a Java Bean class to find its properties.
  133. * </p>
  134. *
  135. * <p>
  136. * Note : This version only supports introspectable bean properties and
  137. * their getter and setter methods. Stand-alone <code>is</code> and
  138. * <code>are</code> methods are not supported.
  139. * </p>
  140. *
  141. * @param beanClass
  142. * the Java Bean class to get properties for.
  143. * @return an ordered map from property names to property descriptors
  144. */
  145. static LinkedHashMap<String, PropertyDescriptor> getPropertyDescriptors(
  146. final Class<?> beanClass) {
  147. final LinkedHashMap<String, PropertyDescriptor> pdMap = new LinkedHashMap<String, PropertyDescriptor>();
  148. // Try to introspect, if it fails, we just have an empty Item
  149. try {
  150. final BeanInfo info = Introspector.getBeanInfo(beanClass);
  151. final PropertyDescriptor[] pds = info.getPropertyDescriptors();
  152. // Add all the bean properties as MethodProperties to this Item
  153. for (int i = 0; i < pds.length; i++) {
  154. final Method getMethod = pds[i].getReadMethod();
  155. if ((getMethod != null)
  156. && getMethod.getDeclaringClass() != Object.class) {
  157. pdMap.put(pds[i].getName(), pds[i]);
  158. }
  159. }
  160. } catch (final java.beans.IntrospectionException ignored) {
  161. }
  162. return pdMap;
  163. }
  164. /**
  165. * Gets the underlying JavaBean object.
  166. *
  167. * @return the bean object.
  168. */
  169. public Object getBean() {
  170. return bean;
  171. }
  172. }