package com.hchyun.test.test;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 18209
 * @Date 2021/2/9 20:22
 * @Version 1.0
 */
public class Maptest {
    public static <T> void setObject(T t, Map map) {
        Class cl = t.getClass();
        map.forEach((k, v) -> {
            try {
                String name = k.toString().substring(0, 1).toUpperCase() + k.toString().substring(1);
                Method mo = cl.getMethod("get" + name);
                //类型判断可扩展
                if (mo.getReturnType().getName().equals(Integer.class.getName())) {
                    mo = cl.getMethod("set" + name, Integer.class);
                    mo.invoke(t, v);
                } else if (mo.getReturnType().getName().equals(String.class.getName())) {
                    mo = cl.getMethod("set" + name, String.class);
                    mo.invoke(t, v);
                } else if (mo.getReturnType().getName().equals(Long.class.getName())) {
                    mo = cl.getMethod("set" + name, Long.class);
                    mo.invoke(t, v);
                } else if (mo.getReturnType().getName().equals(Double.class.getName())) {
                    mo = cl.getMethod("set" + name, Double.class);
                    mo.invoke(t, v);
                } else if (mo.getReturnType().getName().equals(Date.class.getName())) {
                    mo = cl.getMethod("set" + name, Date.class);
                    mo.invoke(t, v);
                } else if (mo.getReturnType().getName().equals(Float.class.getName())) {
                    mo = cl.getMethod("set" + name, Float.class);
                    mo.invoke(t, v);
                }
            } catch (NoSuchMethodException e) {
                System.out.println("当前字段属性:" + k + ",不在类中");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        });
    }

    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null)
            return null;
        Object obj = beanClass.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                setter.invoke(obj, map.get(property.getName()));
            }
        }
        return obj;
    }

    private static <T> void populate1(Map<String, String> map, T t) throws Exception {

        //拿到  BeanInfo
        BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());

        //通过 beaninfo 获取所有的描述器
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

        for (PropertyDescriptor pd : pds) {

            //获取属性的名字
            String name = pd.getName();

            //判断是否有这个属性
            if (map.containsKey(name)) {
                //获取属性的  写的方法
                Method wMethod = pd.getWriteMethod();
                if (pd.getPropertyType() == int.class) {
                    wMethod.invoke(t, Integer.valueOf(map.get(name)));
                } else if (pd.getPropertyType() == double.class) {
                    wMethod.invoke(t, Double.valueOf(map.get(name)));
                } else {
                    wMethod.invoke(t, map.get(name));
                }
            }

        }
    }

    public static void main(String[] age) throws Exception {
        IneniudBuid buid = new IneniudBuid();
        Map map = new HashMap();
        map.put("id", 123);
        map.put("name", "十大法d师");
        map.put("begnd", "ssdfa");
        map.put("age", 3.32);
        map.put("addess", "为东风qw股份");
        map.put("begnddd", "ssdfa");
//        setObject(buid,map);
        populate1(map, buid);
        System.out.println(buid.toString());
    }
}
