BeanUtilsExtended { 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) { 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; } private  BeanUtilsExtended(); static Map<String, Object> buildObjectAttributeMap(Object bean); static BeanType createBeanFromAttributeMap(Class<BeanType> beanType,
            Map<String, ? extends Object> attributeValues); @SuppressWarnings("unchecked") static Map<String, String> buildStringAttributeMap(Object bean);  }
@SuppressWarnings("unchecked") @Test public void testBuildMapFromMultiValueBean_shouldContainAllValues() throws Exception { BeanWithMultiValues testBean = new BeanWithMultiValues(42, 2.0, 3.1415, 1.4142135); Map<String, Object> map = BeanUtilsExtended.buildObjectAttributeMap(testBean); assertThat((Long) map.get("id"), is(42L)); assertThat((List<Double>) map.get("numbers"), is(Arrays.asList(2.0, 3.1415, 1.4142135))); }
@Test(expected = SecurityException.class) public void testBuildMapFromBeanWithFaultyGetter_shouldThrowException() throws Exception { BeanWithFaultyGetter bean = new BeanWithFaultyGetter(); BeanUtilsExtended.buildObjectAttributeMap(bean); }