/**
 * All rights Reserved, Designed By www.sunxung.com
 * 
 * @Title:BeanCopierUtil.java
 * @Package com.sunxung.component.util
 * @Description:对象属性复制辅助类
 * @version V1.0
 * @Copyright: 2018 www.sunxung.com Inc. All rights reserved.
 */
package com.sunxung.factoring.component.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * @className:BeanCopierUtil
 * @description:对象属性复制辅助类
 */
public class BeanCopierUtil {

    public static void CopyBeanProperties(Object source, Object dest) {
        try {
            // 获取属性
            BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), java.lang.Object.class);
            PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();
            BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), java.lang.Object.class);
            PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();
            for (int i = 0; i < sourceProperty.length; i++) {
                for (int j = 0; j < destProperty.length; j++) {
                    if (sourceProperty[i].getName().equals(destProperty[j].getName())) {
                        // 调用source的getter方法和dest的setter方法
                        if (null != destProperty[j].getWriteMethod()) {
                            destProperty[j].getWriteMethod().invoke(dest,
                                    sourceProperty[i].getReadMethod().invoke(source));
                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
