/**
*
* @Description: 将bean转换成map
* @Author: vdi100
* @CreateDate: 2019/4/23 14:28
* @Version: 1.0
* @param: condition
* @return:Map<String, Object>
*/
public static Map<String, Object> convertBeanToMap(Object condition) {
if (condition == null) {
return null;
}
if (condition instanceof Map) {
return (Map<String, Object>) condition;
}
Map<String, Object> objectAsMap = new HashMap<String, Object>();
BeanInfo info = null;
try {
info = Introspector.getBeanInfo(condition.getClass());
} catch (IntrospectionException e) {
//e.printStackTrace();
LOGGER.error(e.getMessage());
}
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null && !"class".equals(pd.getName())) {
try {
objectAsMap.put(pd.getName(), reader.invoke(condition));
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
}
return objectAsMap;
}