package com.prolog.cs.biz.outbound.utils;


import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.ClassUtils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * map
 * Created by dlt on 2021-03-25.
 */
public class MapBeanUtils {
    public static Map<String, Object> convertBean(Object o) {
        try {
            List<Field> fields = ClassUtils.getFields(o.getClass());
            Map<String, Object> map = new HashMap();
            Iterator var4 = fields.iterator();

            while (var4.hasNext()) {
                Field f = (Field) var4.next();
                PropertyDescriptor pd = null;

                pd = new PropertyDescriptor(f.getName(), o.getClass());

                Method getMethod = pd.getReadMethod();
                Object v = getMethod.invoke(o);
                if (v != null) {
                    map.put(f.getName(), v);
                }
            }

            return map;
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
            throw BizException.newInstance(e.getMessage());
        }
    }

}