JavaBean及其JavaList复制,为null值的属性不复制 util

本文介绍了一个用于JavaBean属性复制的实用工具类,包括单个对象及集合对象的属性复制方法,通过反射机制实现,适用于Java后端开发场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1前言:没有前言
2废话不多说,直接上代码

package com.xm.hardwaremanagement.util.tkmybatis;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * javabean复制
 *
 * @author lys
 * @date: 3:00 下午 2020/4/22
 * @version: 1.0.0
 */
public class BeanUtils {
    public BeanUtils() {
    }

    /**
     *copy对象
     * @param from  被copy的对象
     * @param toClass  被粘贴的类对象
     * @param <T>
     * @return copy好的对象
     * @throws RuntimeException
     */
    public static <T> T copyProperties(Object from, Class<T> toClass) throws RuntimeException {
        if (from == null) {
            return null;
        } else {
            try {
                T to = toClass.newInstance();
                copyProperties(from, to);
                return to;
            } catch (IllegalAccessException | InstantiationException var4) {
                throw new RuntimeException(var4);
            }
        }
    }

    /**
     * copy集合
     * @param fromList
     * @param toClass
     * @param <T>
     * @return
     * @throws RuntimeException
     */
    public static <T> List<T> copyListProperties(Collection<? extends Object> fromList, Class<T> toClass) throws RuntimeException {
        if (fromList == null) {
            return null;
        } else {
            List<T> result = new ArrayList(fromList.size());
            Iterator var3 = fromList.iterator();

            while (var3.hasNext()) {
                Object from = var3.next();
                T to = copyProperties(from, toClass);
                result.add(to);
            }

            return result;
        }
    }

    /**
     * @author lys
     * @date: 3:01 下午 2020/4/22
     * @return: java.beans.PropertyDescriptor
     * @version: 1.0.0
     * 获取类属性
     */
    private static PropertyDescriptor getPropertyDescriptor(PropertyDescriptor[] pds, PropertyDescriptor ref, boolean isStrict) {
        if (ref.getDisplayName().equals("class")) {
            return null;
        } else {
            PropertyDescriptor[] var3 = pds;
            int var4 = pds.length;

            for (int var5 = 0; var5 < var4; ++var5) {
                PropertyDescriptor pd = var3[var5];
                if (isStrict) {
                    if (pd.equals(ref)) {
                        return pd;
                    }
                } else if (ref.getPropertyType().equals(pd.getPropertyType()) && pd.getName().equals(ref.getName())) {
                    return pd;
                }
            }

            return null;
        }
    }

    /**
     * 合并两个javaBean属性值
     *
     * @author lys
     * @date: 3:01 下午 2020/4/22
     * @return: void
     * @version: 1.0.0
     */
    public static void copyProperties(Object fromObj, Object toObj) throws RuntimeException {
        copyProperties(fromObj, toObj, true);
    }


    /**
     * 对象属性copy
     * @param fromObj 被copy的对象
     * @param toObj  被粘贴的对象
     * @param ignoreNull  true表示不copy为null的属性,false表示都copy
     * @throws RuntimeException
     */
    public static void copyProperties(Object fromObj, Object toObj, boolean ignoreNull) throws RuntimeException {
        if (fromObj != null && toObj != null) {
            Class<? extends Object> fromClass = fromObj.getClass();
            Class<? extends Object> toClass = toObj.getClass();
            boolean isStrict = fromClass == toClass;

            try {
                BeanInfo fromBean = Introspector.getBeanInfo(fromClass);
                BeanInfo toBean = Introspector.getBeanInfo(toClass);
                PropertyDescriptor[] toPds = toBean.getPropertyDescriptors();
                PropertyDescriptor[] fromPds = fromBean.getPropertyDescriptors();
                PropertyDescriptor[] var10 = toPds;
                int var11 = toPds.length;

                for (int var12 = 0; var12 < var11; ++var12) {
                    PropertyDescriptor toPd = var10[var12];
                    PropertyDescriptor fromPd = getPropertyDescriptor(fromPds, toPd, isStrict);
                    if (fromPd != null && fromPd.getDisplayName().equals(toPd.getDisplayName())) {
                        Method readMethod = fromPd.getReadMethod();
                        Method writeMethod = writeMethod(toClass, toPd);
                        if (writeMethod != null && readMethod != null) {
                            Object param = readMethod.invoke(fromObj, (Object[]) null);
                            if (!ignoreNull || param != null) {
                                writeMethod.invoke(toObj, param);
                            }
                        }
                    }
                }

            } catch (Exception var18) {
                throw new RuntimeException(var18);
            }
        }
    }


    public static Method writeMethod(Class<?> clz, PropertyDescriptor property) {
        String firstByte = property.getDisplayName().substring(0, 1).toUpperCase();
        String setMethodStr = "set" + firstByte + property.getDisplayName().substring(1);
        Method method = null;

        try {
            method = clz.getMethod(setMethodStr, property.getPropertyType());
        } catch (NoSuchMethodException var6) {
        }

        return method;
    }
}

  • 完结
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值