package com.xt.hb.data.util;

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

/**
 * Created by 马英虎 on 2014/11/27.
 */
public class BeanUtil {

    public static Map<String, Object> bean2Map(Object obj) {

        if(obj == null){
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            System.out.println("bean2Map Error " + e);
        }
        return map;
    }

    public static void map2Bean(Map<String, Object> map, Object obj) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                if (map.containsKey(key.toLowerCase()) || map.containsKey(key.toUpperCase())) {
                    Object value = map.get(key);
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }
            }
        } catch (Exception e) {
            System.out.println("map2Bean Error " + e);
        }
    }

    public static void bean2Bean(Object source,Object target){
        try {
            BeanInfo sourceInfo = Introspector.getBeanInfo(source.getClass());
            BeanInfo targetInfo = Introspector.getBeanInfo(target.getClass());
            PropertyDescriptor[] sourceInfoPropertyDescriptors = sourceInfo.getPropertyDescriptors();
            PropertyDescriptor[] targetInfoPropertyDescriptors = targetInfo.getPropertyDescriptors();

            for (PropertyDescriptor sourceProperty : sourceInfoPropertyDescriptors) {
                String sourceKey = sourceProperty.getName();
                for (PropertyDescriptor targetProperty : targetInfoPropertyDescriptors) {

                    if(sourceKey.equalsIgnoreCase(targetProperty.getName())  && !sourceKey.equals("class")){
                        targetProperty.getWriteMethod().invoke(target,sourceProperty.getReadMethod().invoke(source));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("map2Bean Error " + e);
        }
    }

}
