object转map类型

/**
 * 将一个 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;
}

  

posted @ 2019-12-10 10:39  o_osky  阅读(5619)  评论(0编辑  收藏  举报