使用反射拷贝对象指定属性,获取对象属性值,设置目标属性值的反射操作工具类

下面是一个利用反射操作bean的工具类。
实现功能:

  1. 拷贝对象属性;
  2. 拷贝指定属性;
  3. 设置属性;
  4. 获取属性;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;

import org.apache.commons.lang.StringUtils;

/**
 * 将一个对象的属性值拷贝到另一个类或者对象对应属性中
 * 属性 名及类型要匹配 并且 源对象属性要提供getter方法  目标对象属性要提供setter方法
 */
public class BeansUtil {
    /**
     * 拷贝属性
     *
     * @param sourceObj   源对象
     * @param targetClass 目标类
     * @return
     */
    public static Object copyProperties(Object sourceObj, Class<?> targetClass)
    					 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        Object targetObj = Class.forName(targetClass.getName()).newInstance();
        return copyProperties(sourceObj, targetObj);
    }

    /**
     * 拷贝对象属性
     *
     * @param sourceObj 源对象
     * @param targetObj 目标对象
     * @return
     */
    public static Object copyProperties(Object sourceObj, Object targetObj) {
        if (sourceObj == null) {
            throw new IllegalArgumentException("sourceObj is null");
        }
        if (targetObj == null) {
            throw new IllegalArgumentException("targetObj is null");
        }

        Field[] targetFields = targetObj.getClass().getDeclaredFields();
        if (targetFields.length == 0) {
            return targetObj;
        }

        for (int i = 0; i < targetFields.length; i++) {
            copyProperty(sourceObj, targetObj, targetFields[i].getName());
        }

        return targetObj;
    }

    /**
     * 拷贝指定的对象属性
     *
     * @param sourceObj
     * @param targetObj
     * @param copyFields 指定的对象属性
     * @return
     */
    public static Object copyProperties(Object sourceObj, Object targetObj, String[] copyFields) {
        if (sourceObj == null) {
            throw new IllegalArgumentException("sourceObj is null");
        }
        if (targetObj == null) {
            throw new IllegalArgumentException("targetObj is null");
        }
        if (copyFields == null || copyFields.length <= 0) {
            return targetObj;
        }

        Field[] targetFields = targetObj.getClass().getDeclaredFields();
        if (targetFields.length == 0) {
            return targetObj;
        }

        for (int i = 0; i < copyFields.length; i++) {
            copyProperty(sourceObj, targetObj, copyFields[i]);
        }
        return targetObj;
    }

    //PropertyDescriptor内部已经自动处理了is get的情况
    public static void copyProperty(Object sourceObj, Object targetObj, String name) {
        if (sourceObj == null || targetObj == null || StringUtils.isBlank(name)) {
            return;
        }

        try {
            Object value = getProperty(sourceObj, name);
            setProperty(targetObj, name, value);
        } catch (Exception e) {

        }
    }

    public static void setProperty(Object object, String name, Object value) throws Exception {
        if (object == null || StringUtils.isBlank(name)) {
            return;
        }

        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, object.getClass());
        propertyDescriptor.getWriteMethod().invoke(object, value);
    }

    //PropertyDescriptor内部已经自动处理了is get的情况
    public static Object getProperty(Object object, String name) throws Exception {
        if (object == null || StringUtils.isBlank(name)) {
            return null;
        }

        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, object.getClass());
        return propertyDescriptor.getReadMethod().invoke(object);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值