内省 Instrospector

public void populate(Map<String, String[]> map, Person user) throws Exception {
        BeanInfo info = Introspector.getBeanInfo(user.getClass());
        // 获取属性的描述
        PropertyDescriptor [] pds = info.getPropertyDescriptors();
        // 循环遍历
        for (PropertyDescriptor pd : pds) {
            // 获取到属性的名称
            String name = pd.getName();
            // map的key    
            if(map.containsKey(name)){
                // 获取属性的写的方法
                Method m = pd.getWriteMethod();
                System.out.println(m.getName());
                // 执行的时候注意类型转化  如果是  date 强转会出错的 因此在转之前需要判断
                m.invoke(user, map.get(name)[0]);
            }
        }
    }
原文地址:https://www.cnblogs.com/xh0626/p/5602380.html