javaBean的工具类--将两个对象中的相同字段进行复制赋值

package com.elane.utils;

import java.lang.reflect.Field;

/**
 * @Author: yl
 * @Despriction: javaBean的工具类
 * @Package: com.elane.utils
 * @Date:Created in 2019/5/11 15:42
 * @Modify By:
 */
public class PojoUtils {
    /**
     * 赋值给同类对象
     * 将origin属性注入到destination中
     * @param origin
     * @param destination
     * @param 
     */
    public  void mergeObject(T origin, T destination) {
        if (origin == null || destination == null)
            return;
        if (!origin.getClass().equals(destination.getClass()))
            return;

        Field[] fields = origin.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            try {
                fields[i].setAccessible(true);
                Object value = fields[i].get(origin);
                if (null != value && !"".equals(value)) {
                    fields[i].set(destination, value);
                }
                fields[i].setAccessible(false);
            } catch (Exception e) {
            }
        }
    }
}

你可能感兴趣的:(springboot)