java Map與Bean相互轉化

來源:互聯網
上載者:User

標籤:public   lang   int   return   tcl   str   .property   key   reflect   

import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;/** * Created by FengZhen on 17/3/8. */public class MapToBeanUtil {    /**     * 將一個 Map 對象轉化為一個 JavaBean     * @param type 要轉化的類型     * @param map 包含屬性值的 map     * @return 轉化出來的 JavaBean 對象     * @throws IntrospectionException     *             如果分析類屬性失敗     * @throws IllegalAccessException     *             如果執行個體化 JavaBean 失敗     * @throws InstantiationException     *             如果執行個體化 JavaBean 失敗     * @throws InvocationTargetException     *             如果調用屬性的 setter 方法失敗     */    public static Object convertMap(Class type, Map map)            throws IntrospectionException, IllegalAccessException,            InstantiationException, InvocationTargetException {        BeanInfo beanInfo = Introspector.getBeanInfo(type); // 擷取類屬性        Object obj = type.newInstance(); // 建立 JavaBean 對象        // 給 JavaBean 對象的屬性賦值        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();        for (int i = 0; i< propertyDescriptors.length; i++) {            PropertyDescriptor descriptor = propertyDescriptors[i];            String propertyName = descriptor.getName();            if (map.containsKey(propertyName)) {                // 下面一句可以 try 起來,這樣當一個屬性賦值失敗的時候就不會影響其他屬性賦值。                Object value = map.get(propertyName);                Object[] args = new Object[1];                args[0] = value;                descriptor.getWriteMethod().invoke(obj, args);            }        }        return obj;    }    /**     * 將一個 JavaBean 對象轉化為一個  Map     * @param bean 要轉化的JavaBean 對象     * @return 轉化出來的  Map 對象     * @throws IntrospectionException 如果分析類屬性失敗     * @throws IllegalAccessException 如果執行個體化 JavaBean 失敗     * @throws InvocationTargetException 如果調用屬性的 setter 方法失敗     */    public static Map convertBean(Object bean)            throws IntrospectionException, IllegalAccessException, InvocationTargetException {        Class type = bean.getClass();        Map returnMap = new HashMap();        BeanInfo beanInfo = Introspector.getBeanInfo(type);        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();        for (int i = 0; i< propertyDescriptors.length; i++) {            PropertyDescriptor descriptor = propertyDescriptors[i];            String propertyName = descriptor.getName();            if (!propertyName.equals("class")) {                Method readMethod = descriptor.getReadMethod();                Object result = readMethod.invoke(bean, new Object[0]);                if (result != null) {                    returnMap.put(propertyName, result);                } else {                    returnMap.put(propertyName, "");                }            }        }        return returnMap;    }}

 

java Map與Bean相互轉化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.