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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2000-2016 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.InvocationTargetException;
  23. import java.lang.reflect.Method;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.vaadin.data.validator.BeanValidator;
  29. /**
  30. * Utility class for Java Beans information access.
  31. *
  32. * @since 7.4
  33. *
  34. * @author Vaadin Ltd
  35. */
  36. public final class BeanUtil implements Serializable {
  37. // Prevent instantiation of util class
  38. private BeanUtil() {
  39. }
  40. /**
  41. * Returns the property descriptors of a class or an interface.
  42. *
  43. * For an interface, superinterfaces are also iterated as Introspector does
  44. * not take them into account (Oracle Java bug 4275879), but in that case,
  45. * both the setter and the getter for a property must be in the same
  46. * interface and should not be overridden in subinterfaces for the discovery
  47. * to work correctly.
  48. * <p>
  49. * NOTE : This utility method relies on introspection (and returns
  50. * PropertyDescriptor) which is a part of java.beans package. The latter
  51. * package could require bigger JDK in the future (with Java 9+). So it may
  52. * be changed in the future.
  53. * <p>
  54. * For interfaces, the iteration is depth first and the properties of
  55. * superinterfaces are returned before those of their subinterfaces.
  56. *
  57. * @param beanType
  58. * the type whose properties to query
  59. * @return a list of property descriptors of the given type
  60. * @throws IntrospectionException
  61. * if the introspection fails
  62. */
  63. public static List<PropertyDescriptor> getBeanPropertyDescriptors(
  64. final Class<?> beanType) throws IntrospectionException {
  65. // Oracle bug 4275879: Introspector does not consider superinterfaces of
  66. // an interface
  67. if (beanType.isInterface()) {
  68. List<PropertyDescriptor> propertyDescriptors = new ArrayList<>();
  69. for (Class<?> cls : beanType.getInterfaces()) {
  70. propertyDescriptors.addAll(getBeanPropertyDescriptors(cls));
  71. }
  72. BeanInfo info = Introspector.getBeanInfo(beanType);
  73. propertyDescriptors.addAll(getPropertyDescriptors(info));
  74. return propertyDescriptors;
  75. } else {
  76. BeanInfo info = Introspector.getBeanInfo(beanType);
  77. return getPropertyDescriptors(info);
  78. }
  79. }
  80. /**
  81. * Returns the type of the property with the given name and declaring class.
  82. * The property name may refer to a nested property, eg.
  83. * "property.subProperty" or "property.subProperty1.subProperty2". The
  84. * property must have a public read method (or a chain of read methods in
  85. * case of a nested property).
  86. *
  87. * @param beanType
  88. * the type declaring the property
  89. * @param propertyName
  90. * the name of the property
  91. * @return the property type
  92. * @throws IntrospectionException
  93. * if the introspection fails
  94. */
  95. public static Class<?> getPropertyType(Class<?> beanType,
  96. String propertyName) throws IntrospectionException {
  97. PropertyDescriptor descriptor = getPropertyDescriptor(beanType,
  98. propertyName);
  99. if (descriptor != null) {
  100. return descriptor.getPropertyType();
  101. } else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Returns the property descriptor for the property of the given name and
  107. * declaring class. The property name may refer to a nested property, eg.
  108. * "property.subProperty" or "property.subProperty1.subProperty2". The
  109. * property must have a public read method (or a chain of read methods in
  110. * case of a nested property).
  111. *
  112. * @param beanType
  113. * the type declaring the property
  114. * @param propertyName
  115. * the name of the property
  116. * @return the corresponding descriptor
  117. * @throws IntrospectionException
  118. * if the introspection fails
  119. */
  120. public static PropertyDescriptor getPropertyDescriptor(Class<?> beanType,
  121. String propertyName) throws IntrospectionException {
  122. if (propertyName.contains(".")) {
  123. String[] parts = propertyName.split("\\.", 2);
  124. // Get the type of the field in the bean class
  125. Class<?> propertyBean = getPropertyType(beanType, parts[0]);
  126. // Find the rest from the sub type
  127. return getPropertyDescriptor(propertyBean, parts[1]);
  128. } else {
  129. List<PropertyDescriptor> descriptors = getBeanPropertyDescriptors(
  130. beanType);
  131. for (PropertyDescriptor descriptor : descriptors) {
  132. final Method getMethod = descriptor.getReadMethod();
  133. if (descriptor.getName().equals(propertyName)
  134. && getMethod != null
  135. && getMethod.getDeclaringClass() != Object.class) {
  136. return descriptor;
  137. }
  138. }
  139. return null;
  140. }
  141. }
  142. /**
  143. * Returns whether an implementation of JSR-303 version 1.0 or 1.1 is
  144. * present on the classpath. If this method returns false, trying to create
  145. * a {@code BeanValidator} instance will throw an
  146. * {@code IllegalStateException}. If an implementation is not found, logs a
  147. * level {@code FINE} message the first time it is run.
  148. *
  149. * @return {@code true} if bean validation is available, {@code false}
  150. * otherwise.
  151. */
  152. public static boolean checkBeanValidationAvailable() {
  153. return LazyValidationAvailability.BEAN_VALIDATION_AVAILABLE;
  154. }
  155. // Workaround for Java6 bug JDK-6788525. Do nothing for JDK7+.
  156. private static List<PropertyDescriptor> getPropertyDescriptors(
  157. BeanInfo beanInfo) {
  158. PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
  159. List<PropertyDescriptor> result = new ArrayList<>(descriptors.length);
  160. for (PropertyDescriptor descriptor : descriptors) {
  161. try {
  162. Method readMethod = getMethodFromBridge(
  163. descriptor.getReadMethod());
  164. if (readMethod != null) {
  165. Method writeMethod = getMethodFromBridge(
  166. descriptor.getWriteMethod(),
  167. readMethod.getReturnType());
  168. if (writeMethod == null) {
  169. writeMethod = descriptor.getWriteMethod();
  170. }
  171. PropertyDescriptor descr = new PropertyDescriptor(
  172. descriptor.getName(), readMethod, writeMethod);
  173. result.add(descr);
  174. } else {
  175. result.add(descriptor);
  176. }
  177. } catch (SecurityException ignore) {
  178. // handle next descriptor
  179. } catch (IntrospectionException e) {
  180. result.add(descriptor);
  181. }
  182. }
  183. return result;
  184. }
  185. /**
  186. * Return declared method for which {@code bridgeMethod} is generated. If
  187. * {@code bridgeMethod} is not a bridge method then return null.
  188. */
  189. private static Method getMethodFromBridge(Method bridgeMethod)
  190. throws SecurityException {
  191. if (bridgeMethod == null) {
  192. return null;
  193. }
  194. return getMethodFromBridge(bridgeMethod,
  195. bridgeMethod.getParameterTypes());
  196. }
  197. /**
  198. * Return declared method for which {@code bridgeMethod} is generated using
  199. * its {@code paramTypes}. If {@code bridgeMethod} is not a bridge method
  200. * then return null.
  201. */
  202. private static Method getMethodFromBridge(Method bridgeMethod,
  203. Class<?>... paramTypes) throws SecurityException {
  204. if (bridgeMethod == null || !bridgeMethod.isBridge()) {
  205. return null;
  206. }
  207. try {
  208. return bridgeMethod.getDeclaringClass()
  209. .getMethod(bridgeMethod.getName(), paramTypes);
  210. } catch (NoSuchMethodException e) {
  211. return null;
  212. }
  213. }
  214. private static class LazyValidationAvailability implements Serializable {
  215. private static final boolean BEAN_VALIDATION_AVAILABLE = isAvailable();
  216. private static boolean isAvailable() {
  217. try {
  218. Class<?> clazz = Class.forName("javax.validation.Validation");
  219. Method method = clazz.getMethod("buildDefaultValidatorFactory");
  220. method.invoke(null);
  221. return true;
  222. } catch (ClassNotFoundException | NoSuchMethodException
  223. | InvocationTargetException e) {
  224. Logger.getLogger(BeanValidator.class.getName()).log(Level.INFO,
  225. "A JSR-303 bean validation implementation not found on the classpath or could not be initialized. "
  226. + BeanValidator.class.getSimpleName()
  227. + " cannot be used.",
  228. e);
  229. return false;
  230. } catch (IllegalAccessException | IllegalArgumentException e) {
  231. throw new RuntimeException(
  232. "Unable to invoke javax.validation.Validation.buildDefaultValidatorFactory()",
  233. e);
  234. }
  235. }
  236. private LazyValidationAvailability() {
  237. }
  238. }
  239. }