可以自己继承BeanUtils写一个工具类实现map转化为list
用反射机制,反射时获取字段不能获取其所继承的父类的字段,并且反射和被反射的对象字段名字和类型要一致
List<TbPowerGroup> list = BeanUtils.ListMapToListBean(qr.getDatas(), TbPowerGroup.class);
public Tree getSmallPowerMenu(Map<String, Object> param) throws Exception {
// TODO Auto-generated method stub
List<TbPowerGroupBeanVo> list= (List<TbPowerGroupBeanVo>) BeanUtils.ListMapToListBean(tbPowerGroupBeanDao.getSmallPowerMenu(param), TbPowerGroupBeanVo.class);
Map mr = new HashMap();
mr.put("NodeName", "根节点");
Tree tree= new Tree("-1", 0, mr, 0);
createMenuTree(tree,list);
return tree;
}
package com.esteel.utils;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import com.esteel.web.utils.StringUtils;
public class BeanUtils extends org.apache.commons.beanutils.BeanUtils {
public static final String VAR_SPLIT_CHAR = " ";
public static String objValueToString(Object obj) {
if (obj == null)
return "对象为NULL";
String result = "";
try {
result = JSONUtils.toJSONString(obj);
} catch (Exception e) {
StringBuffer buf = new StringBuffer();
try {
buf.append(obj.getClass().getName());
buf.append("=>");
Method[] methods = obj.getClass().getMethods();
if (methods != null) {
int i = 0;
for (int n = methods.length; i < n; i++) {
try {
if (methods[i].getName().startsWith("get")) {
buf.append(methods[i].getName());
buf.append("=");
Object returnValue = methods[i].invoke(obj, new Object[0]);
buf.append(transeObj(returnValue, methods[i].getReturnType()));
buf.append(" ");
}
} catch (Exception e2) {
}
}
}
} catch (Exception e4) {
e.printStackTrace();
buf.append("ERROR:" + e.getMessage());
}
return buf.toString();
}
return result;
}
public static String classValueToString(Class<?> objClass) {
if (objClass == null)
return "类名为NULL";
StringBuffer buf = new StringBuffer();
try {
buf.append(objClass.getName());
buf.append("=>");
Field[] field = objClass.getFields();
if (field != null) {
int i = 0;
for (int n = field.length; i < n; i++) {
buf.append(field[i].getName());
buf.append("=");
buf.append(transeObj(field[i].get(null), String.class));
buf.append(";");
}
}
} catch (Exception e) {
e.printStackTrace();
buf.append("ERROR:" + e.getMessage());
}
return buf.toString();
}
public static String firstToUpper(String strName) {
if (StringUtils.isNull(strName))
return "";
if (strName.length() > 1) {
char ch1 = strName.charAt(0);
char ch2 = strName.charAt(1);
if ((ch1 >= 'A') && (ch1 <= 'Z'))
if (((ch2 >= 'A' ? 1 : 0) & (ch2 <= 'Z' ? 1 : 0)) != 0) {
return strName;
}
return strName.substring(0, 1).toUpperCase() + strName.substring(1, strName.length());
}
return strName.toUpperCase();
}
public static String firstToLower(String strName) {
if (StringUtils.isNull(strName))
return "";
if (strName.length() > 1) {
char ch1 = strName.charAt(0);
char ch2 = strName.charAt(1);
if ((ch1 >= 'A') && (ch1 <= 'Z'))
if (((ch2 >= 'A' ? 1 : 0) & (ch2 <= 'Z' ? 1 : 0)) != 0) {
return strName;
}
return strName.substring(0, 1).toLowerCase() + strName.substring(1, strName.length());
}
return strName.toLowerCase();
}
public static Object transeObj(Object srcObj, Class<?> targetClass) {
if ((srcObj == null) || (targetClass == null)) {
return null;
}
if ((srcObj != null) && (targetClass.getName().equals(srcObj.getClass().getName()))) {
return srcObj;
}
if ((targetClass.getName().equals("long")) || (targetClass.getName().equals("int"))) {
if (srcObj == null)
return null;
return Integer.valueOf(getValue(srcObj, 0));
}
if (targetClass.getName().equals("java.lang.String")) {
if (srcObj == null)
return null;
return getValue(srcObj, "");
}
if (targetClass.getName().equals("java.lang.Integer")) {
if (srcObj == null) {
return null;
}
return Integer.valueOf(getValue(srcObj, 0));
}
if (targetClass.getName().equals("java.math.BigInteger")) {
if (srcObj == null) {
return null;
}
return BigInteger.valueOf(getValue(srcObj, 0));
}
if (targetClass.getName().equals("java.math.BigDecimal")) {
if (srcObj == null) {
return null;
}
return BigDecimal.valueOf(getValue(srcObj, 0));
}
if (targetClass.getName().equals("char")) {
if (srcObj == null) {
return null;
}
return (Character) srcObj;
}
if (targetClass.getName().equals("java.util.Date")) {
if (srcObj == null) {
return null;
}
return (Date) srcObj;
}
return null;
}
public static int getValue(Object obj, int iDefault) {
if (obj == null)
return iDefault;
int iResult = iDefault;
try {
if ((obj instanceof BigDecimal)) {
iResult = ((BigDecimal) obj).intValue();
} else if ((obj instanceof BigInteger)) {
iResult = ((BigInteger) obj).intValue();
} else if ((obj instanceof Integer)) {
iResult = ((Integer) obj).intValue();
} else if ((obj instanceof Double)) {
iResult = ((Double) obj).intValue();
} else if ((obj instanceof Long)) {
iResult = ((Long) obj).intValue();
}
} catch (Exception e) {
iResult = iDefault;
}
return iResult;
}
public static String getValue(Object obj, String strDefault) {
if (obj == null)
return strDefault;
String result = strDefault;
try {
if ((obj instanceof String)) {
result = (String) obj;
}
} catch (Exception e) {
result = strDefault;
}
return result;
}
public static Object getValue(Object obj) {
if (obj == null)
return "";
Object result = obj;
try {
if ((obj instanceof String)) {
result = StringUtils.trimWhitespace((String) obj);
} else if ((obj instanceof String[])) {
String[] tmpArray = (String[]) obj;
if (tmpArray != null) {
if (tmpArray.length == 1) {
result = tmpArray[0];
} else {
StringBuffer buff = new StringBuffer();
int i = 0;
for (int n = tmpArray.length; i < n; i++) {
buff.append(i).append("=").append(getValue(tmpArray[i]));
}
result = buff.toString();
}
}
} else if ((obj instanceof Integer[])) {
Integer[] tmpArray = (Integer[]) obj;
if (tmpArray != null) {
if (tmpArray.length == 1) {
result = tmpArray[0];
} else {
StringBuffer buff = new StringBuffer();
int i = 0;
for (int n = tmpArray.length; i < n; i++) {
buff.append(i).append("=").append(getValue(tmpArray[i]));
}
result = buff.toString();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void copyInProperties(Object target, Object source, String properties) {
if (!StringUtils.isNull(properties)) {
String[] propertiesArray = StringUtils.tokenizeToStringArray(properties, ",");
copyInProperties(source, target, propertiesArray);
}
}
public static void copyInProperties(Object target, Object source, String[] properties) {
if ((properties == null) || (properties.length == 0))
return;
int i = 0;
for (int n = properties.length; i < n; i++) {
String fieldName = properties[i];
Method getMethod = null;
Method setMethod = null;
try {
String getMethodName = "get" + firstToUpper(fieldName);
getMethod = source.getClass().getMethod(getMethodName, new Class[0]);
} catch (Exception e) {
System.out.println("无法获取" + source.getClass().getName() + "的属性" + fieldName + "方法...");
}
if (getMethod != null) {
try {
String setMethodName = "set" + firstToUpper(fieldName);
setMethod = target.getClass().getMethod(setMethodName, new Class[] { getMethod.getReturnType() });
} catch (Exception e) {
System.out.println("无法获取" + target.getClass().getName() + "的属性" + fieldName + "方法...");
}
if (setMethod != null) {
try {
if ((getMethod != null) && (setMethod != null)) {
Object value = getMethod.invoke(source, new Object[0]);
setMethod.invoke(target, new Object[] { value });
}
} catch (Exception e) {
System.out.println("从" + source.getClass().getName() + "中设置" + target.getClass().getName() + "的属性" + fieldName + "值出现异常" + e.getMessage() + "...");
}
}
}
}
}
public static boolean isNull(Object obj) {
if (obj == null)
return true;
if ((obj instanceof String))
return StringUtils.isNull((String) obj);
if ((obj instanceof Object[])) {
Object[] objs = (Object[]) obj;
if (objs.length > 0) {
return isNull(objs[0]);
}
return true;
}
return obj == null;
}
public static String toString(HttpServletRequest request) {
StringBuffer buff = new StringBuffer();
buff.append("contextPath=").append(request.getContextPath()).append(" ");
buff.append("remotePort=").append(request.getRemotePort()).append(" ");
buff.append("remoteAddr=").append(request.getRemoteAddr()).append(" ");
buff.append("requestURI=").append(request.getRequestURI()).append(" ");
buff.append("queryString=").append(request.getQueryString()).append(" ");
Cookie[] cookies = request.getCookies();
if (cookies != null) {
int i = 0;
for (int n = cookies.length; i < n; i++) {
buff.append("cookieName=").append(cookies[i].getName()).append(" ");
buff.append("cookieValue=").append(cookies[i].getValue()).append(" ");
}
}
Map<String, Object> paraMap = request.getParameterMap();
for (Map.Entry<String, Object> ent : paraMap.entrySet()) {
buff.append("reqName=").append((String) ent.getKey()).append(" ");
buff.append("reqValue=").append(getValue(ent.getValue())).append(" ");
}
return buff.toString();
}
public static String toString(Exception e) {
StringBuffer buff = new StringBuffer();
buff.append(e.getMessage());
buff.append(e.getCause().toString());
return buff.toString();
}
public static Object MapToBean(Map<?, ?> map, Class<?> c)
throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
Object o = c.newInstance();
if ((map == null) || (map.size() == 0) || (c == null)) {
return null;
}
Field[] fields = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
Class<?> fieldType = field.getType();
Class<?>[] parameterTypes = { fieldType };
Method method = c.getMethod("set" + getMethodName(field.getName()), parameterTypes);
Object tvalue = getFieldValue(field, map);
if (tvalue != null) {
Object[] parameterValue = createObjectArr(fieldType.getName(), tvalue);
method.invoke(o, parameterValue);
}
}
return o;
}
private static Object getFieldValue(Field field, Map<?, ?> map) {
try {
return map.get(field.getName().toUpperCase());
} catch (RuntimeException ex) {
}
return null;
}
private static Object[] createObjectArr(String typeName, Object tvalue) {
Object[] returnObjs = null;
try {
if (("int".equals(typeName)) || ("java.lang.Integer".equals(typeName))) {
returnObjs = new Object[] { Integer.valueOf(tvalue.toString()) };
} else if ("java.lang.String".equals(typeName)) {
returnObjs = new Object[] { String.valueOf(tvalue) };
} else if (("float".equals(typeName)) || ("java.lang.Float".equals(typeName))) {
returnObjs = new Object[] { Float.valueOf(tvalue.toString()) };
} else if (("double".equals(typeName)) || ("java.lang.Double".equals(typeName))) {
if ("".equals(tvalue.toString())) {
tvalue = "0";
}
returnObjs = new Object[] { Double.valueOf(tvalue.toString()) };
} else if (("byte".equals(typeName)) || ("java.lang.Byte".equals(typeName))) {
returnObjs = new Object[] { (Byte) tvalue };
} else if (("char".equals(typeName)) || ("java.lang.Character".equals(typeName))) {
returnObjs = new Object[] { (Character) tvalue };
} else if (("boolean".equals(typeName)) || ("java.lang.Boolean".equals(typeName))) {
returnObjs = new Object[] { (Boolean) tvalue };
} else if (("long".equals(typeName)) || ("java.lang.Long".equals(typeName))) {
returnObjs = new Object[] { Long.valueOf(tvalue.toString()) };
} else if (("short".equals(typeName)) || ("java.lang.Short".equals(typeName))) {
returnObjs = new Object[] { Short.valueOf(tvalue.toString()) };
}
return new Object[] { tvalue };
} catch (RuntimeException ex) {
throw ex;
}
}
public static List<?> ListMapToListBean(List<Map<String, Object>> list, Class<?> c)
throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
List<Object> returnList = new ArrayList();
if ((list == null) || (list.size() == 0) || (c == null)) {
return returnList;
}
for (int i = 0; i < list.size(); i++) {
Object tobj = MapToBean((Map) list.get(i), c);
if (tobj != null) {
returnList.add(tobj);
}
}
return returnList;
}
private static String getMethodName(String field) {
try {
if (field.length() > 1) {
String frist = field.substring(0, 1);
String other = field.substring(1);
return frist.toUpperCase() + other;
}
return field.toUpperCase();
} catch (RuntimeException ex) {
throw ex;
}
}
}