package com.cm.common.utils;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: WMapUtil
 * @Description: map工具类
 * @Author: WangGeng
 * @Date: 2019-05-28 13:11
 * @Version: 1.0
 **/
public class WMapUtil {

    /**
     * bean转map
     *
     * @param bean
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> beanToMapObj(Object bean) {
        Map<String, Object> map = new HashMap<>(0);
        Class<?> clazz = bean.getClass();
        try {
            for (Field field : FieldUtils.getAllFields(clazz)) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(bean));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

}
