10059---java反射实现bean的copy

接调用BeanCopy.copy(targetbean, sourcebean)即可。是十几行的代码,两行就能完成。

BeanCopy.java

package com.beancopy;  
import java.lang.reflect.Field;  
  
import java.lang.reflect.InvocationTargetException;  
  
import java.lang.reflect.Method;  
  
/** 
 * @author Bear 
 *  做一个方法,可以将一个JavaBean风格对象的属性值拷贝到另一个对象的同名属性中 (如果不存在同名属性的就不拷贝) 
**/  
  
public class BeanCopy {  
  
    @SuppressWarnings("unchecked")  
    public static void copy(Object target, Object source) throws Exception {  
  
        /* 
         * 分别获得源对象和目标对象的Class类型对象,Class对象是整个反射机制的源头和灵魂! 
         *  
         * Class对象是在类加载的时候产生,保存着类的相关属性,构造器,方法等信息 
         */  
  
        Class sourceClz = source.getClass();  
  
        Class targetClz = target.getClass();  
  
        // 得到Class对象所表征的类的所有属性(包括私有属性)  
  
        Field[] fields = sourceClz.getDeclaredFields();  
  
        if (fields.length == 0) {  
            fields = sourceClz.getSuperclass().getDeclaredFields();  
        }  
        for (int i = 0; i < fields.length; i++) {  
  
            String fieldName = fields[i].getName();  
  
            Field targetField = null;  
  
            // 得到targetClz对象所表征的类的名为fieldName的属性,不存在就进入下次循环  
  
            try {  
                targetField = targetClz.getDeclaredField(fieldName);  
  
            } catch (NoSuchFieldException e) {  
                targetField = targetClz.getSuperclass().getDeclaredField(  
                        fieldName);  
            }  
            // 判断sourceClz字段类型和targetClz同名字段类型是否相同  
  
            if (fields[i].getType() == targetField.getType()) {  
  
                // 由属性名字得到对应get和set方法的名字  
  
                String getMethodName = "get"  
                        + fieldName.substring(0, 1).toUpperCase()  
                        + fieldName.substring(1);  
  
                String setMethodName = "set"  
                        + fieldName.substring(0, 1).toUpperCase()  
                        + fieldName.substring(1);  
  
                // 由方法的名字得到get和set方法的Method对象  
  
                Method getMethod;  
                Method setMethod;  
                try {  
  
                    try {  
                        getMethod = sourceClz.getDeclaredMethod(getMethodName,  
                                new Class[] {});  
  
                    } catch (NoSuchMethodException e) {  
                        getMethod = sourceClz.getSuperclass()  
                                .getDeclaredMethod(getMethodName,  
                                        new Class[] {});  
                    }  
  
                    try {  
                        setMethod = targetClz.getDeclaredMethod(setMethodName,  
                                fields[i].getType());  
  
                    } catch (NoSuchMethodException e) {  
                        setMethod = targetClz.getSuperclass()  
                                .getDeclaredMethod(setMethodName,  
                                        fields[i].getType());  
                    }  
  
                    // 调用source对象的getMethod方法  
  
                    Object result = getMethod.invoke(source, new Object[] {});  
  
                    // 调用target对象的setMethod方法  
  
                    setMethod.invoke(target, result);  
  
                } catch (SecurityException e) {  
                    e.printStackTrace();  
  
                } catch (NoSuchMethodException e) {  
                    e.printStackTrace();  
  
                } catch (IllegalArgumentException e) {  
                    e.printStackTrace();  
  
                } catch (IllegalAccessException e) {  
                    e.printStackTrace();  
  
                } catch (InvocationTargetException e) {  
                    e.printStackTrace();  
  
                }  
  
            } else {  
  
                throw new Exception("同名属性类型不匹配!");  
  
            }  
  
        }  
  
    }  
  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值