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.

BeanUtil.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util;
  17. import java.beans.BeanInfo;
  18. import java.beans.IntrospectionException;
  19. import java.beans.Introspector;
  20. import java.beans.PropertyDescriptor;
  21. import java.io.Serializable;
  22. import java.lang.reflect.Method;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * Utility class for Java Beans information access.
  27. *
  28. * @since 7.4
  29. *
  30. * @author Vaadin Ltd
  31. */
  32. public final class BeanUtil implements Serializable {
  33. // Prevent instantiation of util class
  34. private BeanUtil() {
  35. }
  36. /**
  37. * Returns the property descriptors of a class or an interface.
  38. *
  39. * For an interface, superinterfaces are also iterated as Introspector does
  40. * not take them into account (Oracle Java bug 4275879), but in that case,
  41. * both the setter and the getter for a property must be in the same
  42. * interface and should not be overridden in subinterfaces for the discovery
  43. * to work correctly.
  44. *
  45. * NOTE : This utility method relies on introspection (and returns
  46. * PropertyDescriptor) which is a part of java.beans package. The latter
  47. * package could require bigger JDK in the future (with Java 9+). So it may
  48. * be changed in the future.
  49. *
  50. * For interfaces, the iteration is depth first and the properties of
  51. * superinterfaces are returned before those of their subinterfaces.
  52. *
  53. * @param beanClass
  54. * @return
  55. * @throws IntrospectionException
  56. */
  57. public static List<PropertyDescriptor> getBeanPropertyDescriptor(
  58. final Class<?> beanClass) throws IntrospectionException {
  59. // Oracle bug 4275879: Introspector does not consider superinterfaces of
  60. // an interface
  61. if (beanClass.isInterface()) {
  62. List<PropertyDescriptor> propertyDescriptors = new ArrayList<PropertyDescriptor>();
  63. for (Class<?> cls : beanClass.getInterfaces()) {
  64. propertyDescriptors.addAll(getBeanPropertyDescriptor(cls));
  65. }
  66. BeanInfo info = Introspector.getBeanInfo(beanClass);
  67. propertyDescriptors.addAll(getPropertyDescriptors(info));
  68. return propertyDescriptors;
  69. } else {
  70. BeanInfo info = Introspector.getBeanInfo(beanClass);
  71. return getPropertyDescriptors(info);
  72. }
  73. }
  74. /**
  75. * Returns {@code propertyId} class for property declared in {@code clazz}.
  76. * Property could be of form "property.subProperty[.subProperty2]" i.e.
  77. * refer to some nested property.
  78. *
  79. * @param clazz
  80. * class where property is declared
  81. * @param propertyId
  82. * property of form "property" or
  83. * "property.subProperty[.subProperty2]"
  84. * @return class of the property
  85. * @throws IntrospectionException
  86. */
  87. public static Class<?> getPropertyType(Class<?> clazz, String propertyId)
  88. throws IntrospectionException {
  89. if (propertyId.contains(".")) {
  90. String[] parts = propertyId.split("\\.", 2);
  91. // Get the type of the field in the "cls" class
  92. Class<?> propertyBean = getPropertyType(clazz, parts[0]);
  93. // Find the rest from the sub type
  94. return getPropertyType(propertyBean, parts[1]);
  95. } else {
  96. List<PropertyDescriptor> descriptors = getBeanPropertyDescriptor(clazz);
  97. for (PropertyDescriptor descriptor : descriptors) {
  98. final Method getMethod = descriptor.getReadMethod();
  99. if (descriptor.getName().equals(propertyId)
  100. && getMethod != null
  101. && getMethod.getDeclaringClass() != Object.class) {
  102. return descriptor.getPropertyType();
  103. }
  104. }
  105. return null;
  106. }
  107. }
  108. // Workaround for Java6 bug JDK-6788525. Do nothing for JDK7+.
  109. private static List<PropertyDescriptor> getPropertyDescriptors(
  110. BeanInfo beanInfo) {
  111. PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
  112. List<PropertyDescriptor> result = new ArrayList<PropertyDescriptor>(
  113. descriptors.length);
  114. for (PropertyDescriptor descriptor : descriptors) {
  115. try {
  116. Method readMethod = getMethodFromBridge(descriptor
  117. .getReadMethod());
  118. if (readMethod != null) {
  119. Method writeMethod = getMethodFromBridge(
  120. descriptor.getWriteMethod(),
  121. readMethod.getReturnType());
  122. if (writeMethod == null) {
  123. writeMethod = descriptor.getWriteMethod();
  124. }
  125. PropertyDescriptor descr = new PropertyDescriptor(
  126. descriptor.getName(), readMethod, writeMethod);
  127. result.add(descr);
  128. } else {
  129. result.add(descriptor);
  130. }
  131. } catch (SecurityException ignore) {
  132. // handle next descriptor
  133. } catch (IntrospectionException e) {
  134. result.add(descriptor);
  135. }
  136. }
  137. return result;
  138. }
  139. /**
  140. * Return declared method for which {@code bridgeMethod} is generated. If
  141. * {@code bridgeMethod} is not a bridge method then return null.
  142. */
  143. private static Method getMethodFromBridge(Method bridgeMethod)
  144. throws SecurityException {
  145. if (bridgeMethod == null) {
  146. return null;
  147. }
  148. return getMethodFromBridge(bridgeMethod,
  149. bridgeMethod.getParameterTypes());
  150. }
  151. /**
  152. * Return declared method for which {@code bridgeMethod} is generated using
  153. * its {@code paramTypes}. If {@code bridgeMethod} is not a bridge method
  154. * then return null.
  155. */
  156. private static Method getMethodFromBridge(Method bridgeMethod,
  157. Class<?>... paramTypes) throws SecurityException {
  158. if (bridgeMethod == null || !bridgeMethod.isBridge()) {
  159. return null;
  160. }
  161. try {
  162. return bridgeMethod.getDeclaringClass().getMethod(
  163. bridgeMethod.getName(), paramTypes);
  164. } catch (NoSuchMethodException e) {
  165. return null;
  166. }
  167. }
  168. }