属性拷贝,通过反射把一个类的值传给另外一个类

 

public class ClassUtil {

    /**
     * 适合两个大量相同字段类的赋值
     * {@link Class#newInstance()} 该情况会返回null
     * @param source 数据来源
     * @param targetClass 类及构造函数为public,非抽象类,接口,数据类,原始类型等。
     * @param <T>
     * @return
     */
    public static <T>T copy(Object source,Class<T> targetClass){
        try {
            T target = targetClass.newInstance();
            Class sourceClass = source.getClass();
            Field[] targetFields = targetClass.getDeclaredFields();
            for (Field field : targetFields) {
                String name = field.getName();
                //设为true之后才能对private属性进行操作
                field.setAccessible(true);
                Field sourceField;
                try {
                    sourceField = sourceClass.getDeclaredField(name);
                    sourceField.setAccessible(true);
                } catch (NoSuchFieldException e) {
                    continue;
                }
                Object value = sourceField.get(source);
                if (value != null){
                    if (field.getType().equals(sourceField.getType())){
                        field.set(target, value);
                    }else if (String.class.equals(field.getType())){
                        field.set(target, value.toString());
                    }
                }
            }
            return target;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}

同样,稍微改造一下,就可以给一个已有的实体增加新的值

    public static void copy(Object source,Object target){
        try {
            Class targetClass = target.getClass();
            Class sourceClass = source.getClass();
            Field[] targetFields = targetClass.getDeclaredFields();
            for (Field field : targetFields) {
                String name = field.getName();
                field.setAccessible(true);
                Field sourceField;
                try {
                    sourceField = sourceClass.getDeclaredField(name);
                    sourceField.setAccessible(true);
                } catch (NoSuchFieldException e) {
                    continue;
                }
                Object value = sourceField.get(source);
                if (value != null){
                    if (field.getType().equals(sourceField.getType())){
                        field.set(target, value);
                    }else if (String.class.equals(field.getType())){
                        field.set(target, value.toString());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

这里当字段的类不同时,仅简单做了别的类到String的转换。有兴趣的可以在基础上增加别的转换,如数字之间的转换等。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值