/**
* 将资源Bean中的属性和值获取并保存在Map中
*
* @param javaBean
* 资源Bean
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map<String, Object> inverBeanToMap(Object javaBean) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Map<String, Object> map = new HashMap<String, Object>();
Class<? extends Object> clas = javaBean.getClass();
Method[] methods = clas.getMethods();
if (methods == null || methods.length == 0) {
return map;
}
for (Method method : methods) {
if (method.getName().startsWith("get") || method.getName().startsWith("is")) {// 只处理get和is方法
Object val = method.invoke(javaBean, new Object[] {});
if (val == null || ((val instanceof String) && "".equals(((String) val).trim()))) {// null
continue;
}
String propName = "";
if (method.getName().startsWith("get")) {// 是get方法
propName = method.getName().substring(3, method.getName().length());
} else if (method.getName().startsWith("is")) {// is方法
propName = method.getName().substring(2, method.getName().length());
}
if (propName.length() > 0) {
propName = propName.substring(0, 1).toLowerCase() + propName.substring(1, propName.length());
}
// 获取值
map.put(propName, val);
}
}
return map;
}
* 将资源Bean中的属性和值获取并保存在Map中
*
* @param javaBean
* 资源Bean
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map<String, Object> inverBeanToMap(Object javaBean) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Map<String, Object> map = new HashMap<String, Object>();
Class<? extends Object> clas = javaBean.getClass();
Method[] methods = clas.getMethods();
if (methods == null || methods.length == 0) {
return map;
}
for (Method method : methods) {
if (method.getName().startsWith("get") || method.getName().startsWith("is")) {// 只处理get和is方法
Object val = method.invoke(javaBean, new Object[] {});
if (val == null || ((val instanceof String) && "".equals(((String) val).trim()))) {// null
continue;
}
String propName = "";
if (method.getName().startsWith("get")) {// 是get方法
propName = method.getName().substring(3, method.getName().length());
} else if (method.getName().startsWith("is")) {// is方法
propName = method.getName().substring(2, method.getName().length());
}
if (propName.length() > 0) {
propName = propName.substring(0, 1).toLowerCase() + propName.substring(1, propName.length());
}
// 获取值
map.put(propName, val);
}
}
return map;
}