zoukankan      html  css  js  c++  java
  • 利用反射机制 实现 javabean 转化为 map

    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 + "]";
    	}
    	
    }
    
    
  • 相关阅读:
    14_java之变量|参数|返回值|修饰符
    NYOJ 202 红黑树 (二叉树)
    NYOJ 138 找球号(二) (哈希)
    NYOJ 136 等式 (哈希)
    NYOJ 133 子序列 (离散化)
    NYOJ 129 树的判定 (并查集)
    NYOJ 117 求逆序数 (树状数组)
    NYOJ 93 汉诺塔 (数学)
    HDU 2050 折线分割平面 (数学)
    天梯赛L2-008 最长对称子串 (字符串处理)
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7060780.html
Copyright © 2011-2022 走看看