标签:blog java os io for 2014 ar cti
package com.test.entity; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class ConvertUtil { public static Map fromBean(Object object) throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, ClassNotFoundException { Map<String, Object> map=new HashMap<String, Object>(); Class class1=object.getClass(); Field[] fields=class1.getDeclaredFields(); Method[] methods=class1.getMethods(); for(Field eachField:fields) { String key=eachField.getName();//字段名称 String methodName="get"+key.substring(0, 1).toUpperCase()+key.substring(1);//方法名称 Method method=getMethodByName(methods, methodName);//得到方法 if(eachField.getType().isPrimitive()) { System.out.println(methodName); map.put(key, method.invoke(object, null)); continue; } Class theClass=Class.forName(eachField.getType().getName()); if(!Collection.class.isAssignableFrom(theClass)){//不是集合 if(method==null)//get方法存在 continue; try { Object result=method.invoke(object, null);//执行方法 map.put(key, result);//放入map } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { Collection result=(Collection) method.invoke(object, null); Iterator it=result.iterator(); Object object2=theClass.newInstance(); Collection collection=(Collection<Map>) object2; while (it.hasNext()) { Object object3=it.next(); Map map2=ConvertUtil.fromBean(object3); collection.add(map2); } map.put(key, collection); } } return map; } private static Method getMethodByName(Method[] methods,String name) { for(Method each:methods) { if(each.getName().equals(name)) return each; } return null; } }
package com.test.entity; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Random; public class ConvertUtilTest { public static void main(String[] args) { Demo demo=new Demo(); demo.setStr("abcdefg"); ArrayList list=new ArrayList<demoitem>(); String randomsString="abcdefghijklmnopqrstuvwxyz"; for(int i=0;i<5;i++) { DemoItem demoItem=new DemoItem(); demoItem.setStrr(randomsString.substring(i, i+3)); list.add(demoItem); } demo.setDemoList(list); try { Map map=ConvertUtil.fromBean(demo); System.out.println(map.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </demoitem>
package com.test.entity; import java.util.ArrayList; import java.util.List; public class Demo { private String str; private ArrayList demoList; public String getStr() { return str; } public void setStr(String str) { this.str = str; } public ArrayList getDemoList() { return demoList; } public void setDemoList(ArrayList demoList) { this.demoList = demoList; } @Override public String toString() { return "Demo [demoList=" + demoList + ", str=" + str + "]"; } }
package com.test.entity; public class DemoItem { private String strr; public String getStrr() { return strr; } public void setStrr(String strr) { this.strr = strr; } @Override public String toString() { return "DemoItem [strr=" + strr + "]"; } }
利用反射机制 实现 javabean 转化为 map,布布扣,bubuko.com
标签:blog java os io for 2014 ar cti
原文地址:http://blog.csdn.net/hcmdy/article/details/38394067