1. 程式人生 > >bean to map

bean to map

cep des 邏輯 內部使用 under type 大數據 平臺 有限公司

 1 /**
 2  * @ProjectName: BSP 海康威視大數據服務平臺
 3  * @Copyright: 2015 HangZhou Hikvision System Technology Co., Ltd. All Right Reserved.
 4  * @address: http://www.hikvision.com
 5  * @date: 2015年12月29日 14:44
 6  * @Description: 本內容僅限於杭州海康威視數字技術股份有限公司內部使用,禁止轉發.
 7  */
 8 package com.hikvision.bsp.facecloud.utils;
9 10 import java.beans.BeanInfo; 11 import java.beans.Introspector; 12 import java.beans.PropertyDescriptor; 13 import java.lang.reflect.Method; 14 import java.util.HashMap; 15 import java.util.Map; 16 17 /** 18 * <p> 19 * 對象轉換成Map 20 * </p> 21 * 22 * @author chenxiaoyou 2015年12月29日 14:44
23 * @version V1.0 24 * @modificationHistory=========================邏輯或功能性重大變更記錄 25 * @modify by user: {修改人} ${date} 26 * @modify by reason: {方法名}:{原因} 27 */ 28 public class BeanToMapUtil { 29 30 public static Map<String, Object> convertBean(Object bean) throws Exception { 31 Class type = bean.getClass();
32 Map returnMap = new HashMap(); 33 BeanInfo beanInfo = Introspector.getBeanInfo(type); 34 35 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 36 for (int i = 0; i < propertyDescriptors.length; i++) { 37 PropertyDescriptor descriptor = propertyDescriptors[i]; 38 String propertyName = descriptor.getName(); 39 if (!"class".equals(propertyName)) { 40 Method readMethod = descriptor.getReadMethod(); 41 Object result = readMethod.invoke(bean, new Object[0]); 42 if (result != null) { 43 returnMap.put(propertyName, result); 44 } 45 } 46 } 47 return returnMap; 48 } 49 }

bean to map