public static Map convertBean(Object bean) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
Map returnMap = new HashMap();
Class type = bean.getClass(); // 获得对象的名字
BeanInfo beanInfo = Introspector.getBeanInfo(type);
// 在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件
PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors();
// 获得 beans PropertyDescriptor
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
// 获得所有对象属性值得名字
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
}