All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flyfish.oauth.utils.CopyUtils Maven / Gradle / Ivy

package com.flyfish.oauth.utils;

import org.apache.commons.beanutils.PropertyUtils;

import java.beans.PropertyDescriptor;

public final class CopyUtils {
    /**
     * 拷贝参数
     * entity类型必须和当前类型一致,否则报错
     *
     * @param source      要拷贝的实体
     * @param destination 目标实体
     * @param          泛型
     */
    public static  T copyProps(T source, T destination) {
        PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(source.getClass());
        if (descriptors.length != 0) {
            for (PropertyDescriptor descriptor : descriptors) {
                // javaBean属性名
                String propertyName = descriptor.getName();
                if (!"class".equals(propertyName)) {
                    try {
                        Object value = descriptor.getReadMethod().invoke(source);
                        // 过滤空值,节约带宽
                        if (value != null) {
                            // javaBean属性值
                            descriptor.getWriteMethod().invoke(destination, value);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return destination;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy