将对象转换为指定的Map

根据指定对象内的字段,获取信息放入Map。或者将整个对象打印出来。

package com.chen.Test;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestInvoke
{
    private String c;

    private int num;

    private Date date;
    
    private boolean flag;

    

    public String getC()
    {
        return c;
    }

    public void setC(String c)
    {
        this.c = c;
    }

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }

    public boolean isFlag()
    {
        return flag;
    }

    public void setFlag(boolean flag)
    {
        this.flag = flag;
    }

    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num = num;
    }

    /**根据column到obj对象中获取数值填充Map<br />
     * 
     * @author : EX-CHENWEIXIAN001 路人甲
     * @create_date :2013-5-11 下午06:57:11
     * @param obj
     * @param columns
     * @return
     */
    public static Map fillMapByObject(Object obj, String[] columns) 
    {
        String getMethodStartTag = "get"; // 获取数据的方法名前缀
        String isMethodStartTag = "is"; // boolean获取数据的方法名前缀
        if (obj instanceof Map)
        {
            return (Map)obj;
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Method[] methods = obj.getClass().getMethods();
        for (int i = 0; i < methods.length; i++)
        {
            String key = "";
            boolean flag = false;
            String methodName = methods[i].getName();
            if (methodName.subSequence(0, getMethodStartTag.length()).equals(getMethodStartTag) || methodName.subSequence(0, isMethodStartTag.length()).equals(isMethodStartTag))
            {
                String tempName = "";
//                 获取is后面的字段名称 
                if (methodName.indexOf(isMethodStartTag) == 0){
                    tempName = methodName.substring(isMethodStartTag.length(), methodName.length());
                }
                // 获取get后面的字段名称 
                else {
                    tempName = methodName.substring(getMethodStartTag.length(), methodName.length());
                }
                for (int j = 0; j < columns.length; j++)
                {
                    if (tempName.equalsIgnoreCase(columns[j]))
                    {
                        key = columns[j];
                        flag = true;
                        break;
                    }
                }
            }
            if (flag)
            {
                Object val = null;
                try
                {
//                     调用getter方法获取属性值
                    val = methods[i].invoke(obj);
                } catch (Exception e)
                {
                }
                resultMap.put(key, val);
            }
        }
        return resultMap;
    }
    
    /**对象转换为Map<br />
     * 
     * @author : EX-CHENWEIXIAN001 路人甲
     * @create_date :2013-5-11 下午06:57:11
     * @param obj
     * @param columns
     * @return
     */
    public static Map fillMapByObject(Object obj) 
    {
        String getMethodStartTag = "get"; // 获取数据的方法名前缀
        String isMethodStartTag = "is"; // boolean获取数据的方法名前缀
        if (obj instanceof Map)
        {
            return (Map)obj;
        }
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Method[] methods = obj.getClass().getMethods();
        for (int i = 0; i < methods.length; i++)
        {
            String methodName = methods[i].getName();
            // 获取所有get方法
            if (methodName.subSequence(0, getMethodStartTag.length()).equals(getMethodStartTag) || methodName.subSequence(0, isMethodStartTag.length()).equals(isMethodStartTag))
            {
                String tempName = "";
//                 获取is后面的字段名称 
                if (methodName.indexOf(isMethodStartTag) == 0){
                    tempName = methodName.substring(isMethodStartTag.length(), methodName.length());
                }
                // 获取get后面的字段名称 
                else {
                    tempName = methodName.substring(getMethodStartTag.length(), methodName.length());
                }
                // 将第一个字符转换为小写
                String firstName = tempName.substring(0, 1).toLowerCase();
                String lastName = "";
                if (tempName.length() > 1)
                {
                    lastName = tempName.substring(1, tempName.length());
                }
                tempName = firstName + lastName;
                Object val = null;
                try
                {
//                     调用getter方法获取属性值
                    val = methods[i].invoke(obj);
                } catch (Exception e)
                {
                }
                resultMap.put(tempName, val);
            }
        }
        return resultMap;
    }
    
    /**
     * @author : EX-CHENWEIXIAN001 陈惟鲜
     * @create_date :2013-5-11 下午06:25:17
     * @param args
     */
    public static void main(String[] args)
    {
        String[] columns = { "c", "flag", "TAB" };
        TestInvoke t = new TestInvoke();
        t.setC("cc");
        t.setDate(new Date());
        t.setNum(28);
        
        Map mapTemp = new HashMap();
        mapTemp.put("id", "aa");
        mapTemp.put("ggg", "bb");

        System.out.println(fillMapByObject(t, columns));
        System.out.println(fillMapByObject(mapTemp, columns));
        System.out.println(fillMapByObject(t));
    }
}

 

 

posted on 2013-05-13 09:19  陈惟鲜的博客  阅读(291)  评论(0编辑  收藏  举报

导航