java 用反射簡單應用,將Object簡單轉換成map

來源:互聯網
上載者:User

標籤:

package com.appdev.bsf.server.common;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class ObjectDynamicCreator {    /**     * 返回由對象的屬性為key,值為map的value的Map集合     *      * @param obj     *            Object     * @return mapValue Map<String,String>     * @throws Exception     */    public static Map<String, String> getFieldVlaue(Object obj) throws Exception {        Map<String, String> mapValue = new HashMap<String, String>();        Class<?> cls = obj.getClass();        Field[] fields = cls.getDeclaredFields();        for (Field field : fields) {            String name = field.getName();            String strGet = "get" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());            Method methodGet = cls.getDeclaredMethod(strGet);            Object object = methodGet.invoke(obj);            String value = object != null ? object.toString() : "";            mapValue.put(name, value);        }        return mapValue;    }    /**     * 返回由Map的key對屬性,value對應值組成的對應     *      * @param map     *            Map<String,String>     * @param cls     *            Class     * @return obj Object     * @throws Exception     */    public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception {        Field[] fields = cls.getDeclaredFields();        Object obj = cls.newInstance();        for (Field field : fields) {            Class<?> clsType = field.getType();            String name = field.getName();            String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());            Method methodSet = cls.getDeclaredMethod(strSet, clsType);            if (map.containsKey(name)) {                Object objValue = typeConversion(clsType, map.get(name));                methodSet.invoke(obj, objValue);            }        }        return obj;    }    /**     * 將Map裡面的部分值通過反射設定到已有對象裡去     *      * @param obj     *            Object     * @param data     *            Map<String,String>     * @return obj Object     * @throws Exception     */    public static Object setObjectFileValue(Object obj, Map<String, String> data) throws Exception {        Class<?> cls = obj.getClass();        Field[] fields = cls.getDeclaredFields();        for (Field field : fields) {            Class<?> clsType = field.getType();            String name = field.getName();            String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());            Method methodSet = cls.getDeclaredMethod(strSet, clsType);            if (data.containsKey(name)) {                Object objValue = typeConversion(clsType, data.get(name));                methodSet.invoke(obj, objValue);            }        }        return obj;    }    /**     * 把對象的值用Map對應裝起來     *      * @param map     *            Map<String,String>     * @param obj     *            Object     * @return 與對象屬性對應的Map Map<String,String>     */    public static Map<String, String> compareMap(Map<String, String> map, Object obj) {        Map<String, String> mapValue = new HashMap<String, String>();        Field[] fields = obj.getClass().getDeclaredFields();        for (Field field : fields) {            String name = field.getName();            if (map.containsKey(name)) {                mapValue.put(name, map.get(name));            }        }        return mapValue;    }    /**     * 把臨時對象的值複製到持久化對象上     *      * @param oldObject     *            Object 持久化對象     * @param newObject     *            Object 臨時對象     * @return 持久化對象     * @throws Exception     */    public static Object mergedObject(Object oldObject, Object newObject) throws Exception {        Class<?> cls = newObject.getClass();        Field[] fields = cls.getDeclaredFields();        for (Field field : fields) {            Class<?> clsType = field.getType();            String name = field.getName();            String method = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());            String strGet = "get" + method;            Method methodGet = cls.getDeclaredMethod(strGet);            Object object = methodGet.invoke(newObject);            if (object != null) {                String strSet = "set" + method;                Method methodSet = cls.getDeclaredMethod(strSet, clsType);                Object objValue = typeConversion(clsType, object.toString());                methodSet.invoke(oldObject, objValue);            }        }        return oldObject;    }    public static Object typeConversion(Class<?> cls, String str) {        Object obj = null;        String nameType = cls.getSimpleName();        if ("Integer".equals(nameType)) {            obj = Integer.valueOf(str);        }        if ("String".equals(nameType)) {            obj = str;        }        if ("Float".equals(nameType)) {            obj = Float.valueOf(str);        }        if ("Double".equals(nameType)) {            obj = Double.valueOf(str);        }        if ("Boolean".equals(nameType)) {            obj = Boolean.valueOf(str);        }        if ("Long".equals(nameType)) {            obj = Long.valueOf(str);        }        if ("Short".equals(nameType)) {            obj = Short.valueOf(str);        }        if ("Character".equals(nameType)) {            obj = str.charAt(1);        }        return obj;    }}



java 用反射簡單應用,將Object簡單轉換成map

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.