利用反射实现类的复制

利用反射实现类的复制

笔者年前在项目中遇到数据复制报错,根据排查,最终锁定问题出在类的复制上面。经过多种尝试,仍不行,遂放弃common.lang包中的办法,利用反射写个类复制的工具类。闲话不多说,直接上代码。

package com.xq.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
 * 类的拷贝
 * @author wangweiqiang
 *
*/
public class BeanProperties {

    public static void copy(Object target, Object source) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{


        Class sourceClass = source.getClass();
        Class targetClass = target.getClass();

        //获取类的所有属性
        Field[] sourceFields = sourceClass.getDeclaredFields();
        Field[] targetFields = targetClass.getDeclaredFields();

        //双重循环 复制
        for (Field sourceField : sourceFields) {

            String sourceFieldName = sourceField.getName();
            Class sourceFieldType = sourceField.getType();

            //拼写get方法名
            String methodName = sourceFieldName.substring(0, 1).toUpperCase()+sourceFieldName.substring(1);

            Method getMethod = sourceClass.getMethod("get"+methodName);
            //反射获取属性值
            Object value = getMethod.invoke(source);

             for (Field field : targetFields) {
                String targetFieldname = field.getName();

                //判断目标属性的名字和类型 是否一致
                if(targetFieldname.equals(sourceFieldName) && sourceFieldType.equals(field.getType())){
                 Method setMethod = targetClass.getMethod("set"+methodName, sourceFieldType);
                //invoke 执行set方法
                 setMethod.invoke(target, value);
                }
            }

        }

    }
}

测试类

package com.xq.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import com.xq.entity.UserSource;
import com.xq.entity.UserTarget;
import com.xq.util.BeanProperties;
/**
* 测试类
* @author Administrator
*
*/
public class BeanCopyDemo {

public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    UserTarget target = new UserTarget();
    UserSource source = new UserSource();

    source.setAddress("水电费水电费");
    source.setAge(18);
    source.setBirth(new Date());
    source.setUserName("测试反射");
    BeanProperties.copy(target, source);
    System.out.println("name:"+target.getUserName());
    System.out.println("sex:"+target.getSex());
    System.out.println("birth:"+target.getBirth());
    System.out.println("address:"+target.getAddress());
    }
}

下面贴一下实体类

package com.xq.entity;

import java.util.Date;
public class UserTarget {   
    private String userName;        
    private String address;     
    private String sex;     
    private Date birth; 

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }


    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

测试结果:

name:测试反射
sex:null
birth:Sat Jan 07 00:31:06 CST 2017
address:水电费水电费

在此感谢一位朋友,因为你的支持,以后我会坚持更新博客。

posted @ 2017-01-07 00:45  指尖逆苍穹  阅读(400)  评论(0编辑  收藏  举报
AmazingCounters.com