Bean同属性拷贝功能实现

Bean同属性拷贝功能实现

暂且记录,之后再探讨实现细节

package com.dingrui.testboot.bean;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author dingrui
 * @date 2021/3/5 11:39
 * @description
 * @className BeanUtil
 */
public class BeanUtil {

    public static Object Copy(Object obje, Object sour, boolean isCover) {
        Field[] fields = sour.getClass().getDeclaredFields();
        int i = 0;

        for(int j = fields.length; i < j; ++i) {
            String propertyName = fields[i].getName();
            Object propertyValue = getProperty(sour, propertyName);
            if (isCover) {
                if (getProperty(obje, propertyName) == null && propertyValue != null) {
                    setProperty(obje, propertyName, propertyValue);
                }
            } else {
                setProperty(obje, propertyName, propertyValue);
            }
        }

        return obje;
    }

    private static Object getProperty(Object bean, String propertyName) {
        Class clazz = bean.getClass();

        try {
            Field field = clazz.getDeclaredField(propertyName);
            Method method = clazz.getDeclaredMethod(getGetterName(field.getName()));
            return method.invoke(bean);
        } catch (Exception var5) {
            return null;
        }
    }

    private static Object setProperty(Object bean, String propertyName, Object value) {
        Class clazz = bean.getClass();

        try {
            Field field = clazz.getDeclaredField(propertyName);
            Method method = clazz.getDeclaredMethod(getSetterName(field.getName()), field.getType());
            return method.invoke(bean, value);
        } catch (Exception var6) {
            return null;
        }
    }

    private static String getSetterName(String propertyName) {
        String method = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        return method;
    }

    private static String getGetterName(String propertyName) {
        String method = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        return method;
    }

}

测试方法:具体的po类我就不列出来了

   @Test
    void test1() {
        BeanPO beanPO = new BeanPO();
        beanPO.setId(1);
        beanPO.setName("ceshi");

        List<ValidPO> validPOS = new ArrayList<>();
        ValidPO validPO = new ValidPO();
        validPO.setId(22);
        validPO.setName("valid_ceshi");
        validPOS.add(validPO);
        beanPO.setItem(validPOS);

        BeanVO beanVO = new BeanVO();
        beanVO.setId(2);
        BeanUtil.Copy(beanVO, beanPO, false);
        System.out.println(beanVO.toString());
        beanPO.setId(3);
        System.out.println(beanPO.toString());
        System.out.println(beanVO.toString());
    }

测试结果:

BeanVO{id=1, name='ceshi', item=[ValidPO{name='valid_ceshi', id=22}]}
BeanPO{id=3, name='ceshi', item=[ValidPO{name='valid_ceshi', id=22}]}
BeanVO{id=1, name='ceshi', item=[ValidPO{name='valid_ceshi', id=22}]}
posted @ 2021-03-10 14:45  CodeSweet  阅读(85)  评论(0编辑  收藏  举报