Пример #1
0
 /**
  * Analyzes the bean and returns a map containing the property-values. Works similar to {@link
  * BeanUtils#describe(Object)} but does not convert everything to strings.
  *
  * @throws IllegalArgumentException if the bean cannot be analyzed properly. Probably because some
  *     getter throw an Exception
  */
 public static Map<String, Object> buildObjectAttributeMap(Object bean)
     throws IllegalArgumentException {
   BeanInfo beanInfo;
   try {
     beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
   } catch (IntrospectionException e1) {
     throw new IllegalArgumentException(e1);
   }
   Map<String, Object> result;
   result = Maps.newHashMap();
   for (PropertyDescriptor pDesc : beanInfo.getPropertyDescriptors()) {
     String name = pDesc.getName();
     Object propertyValue;
     try {
       propertyValue = pDesc.getReadMethod().invoke(bean);
     } catch (IllegalAccessException e) {
       // this should never happen since the Introspector only returns accessible read-methods
       LOGGER.error("WTF: got property descriptor with inaccessible read-method");
       throw new IllegalStateException(e);
     } catch (InvocationTargetException e) {
       ReflectionUtils.handleInvocationTargetException(e);
       throw new IllegalStateException("Should never get here");
     }
     if (propertyValue != null) {
       result.put(name, propertyValue);
     }
   }
   return result;
 }