类属性拷贝方法

package com.sosgps.dao.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


//类属性复制方法类
public class ClassCopyUtil {
    
    public static Object copy(Object object,Object copyObject) throws Exception{
        return copy(object,copyObject,false);
    }
    /**
     * 对于父类的属性   默认找2层父子关系
     * @param object   提供数据的类对象
     * @param copyObject  需要进行替换的类对象
     * @param filteNullValue  true 对非空属性进行赋值   false不对属性是否为空判断
     * @return
     * @throws Exception
     */
     public static Object copy(Object object,Object copyObject,Boolean filteNullValue){
            Class<?> classType = object.getClass();
            Class<?> classType2 = copyObject.getClass();
            
            //获得所有成员变量
            Field[] fields = classType2.getDeclaredFields();
            for(Field field : fields){
                //获取成员变量的名字
                String name = field.getName();    //获取成员变量的名字,此处为id,name,age
                //获取get和set方法的名字
                String firstLetter = name.substring(0,1).toUpperCase();    //将属性的首字母转换为大写            
                String getMethodName = "get" + firstLetter + name.substring(1);
                String setMethodName = "set" + firstLetter + name.substring(1);            
                //System.out.println(getMethodName + "," + setMethodName);
                try{
                    //获取方法对象
                    Method getMethod = classType.getMethod(getMethodName, new Class[]{});
                    Method setMethod = classType2.getMethod(setMethodName, new Class[]{field.getType()});//注意set方法需要传入参数类型
                    
                    //调用get方法获取旧的对象的值
                    Object value = getMethod.invoke(object, new Object[]{});
                    if(!filteNullValue || (filteNullValue && value != null)){
                          //调用set方法将这个值复制到新的对象中去
                            setMethod.invoke(copyObject, new Object[]{value});
                    }
                }catch(Exception e){
                    //没有getset方法的属性会报错空指针,不影响,不复制该属性
                }
            }
            
            //获得父类所有成员变量
            Class<?> superClassType2 = classType2.getSuperclass();
            if(superClassType2==null){
                return copyObject;
            }
            Field[] superfields = superClassType2.getDeclaredFields();
            for(Field field : superfields){
                //获取成员变量的名字
                String name = field.getName();    //获取成员变量的名字,此处为id,name,age
                //获取get和set方法的名字
                String firstLetter = name.substring(0,1).toUpperCase();    //将属性的首字母转换为大写            
                String getMethodName = "get" + firstLetter + name.substring(1);
                String setMethodName = "set" + firstLetter + name.substring(1);            
                //System.out.println(getMethodName + "," + setMethodName);
                try{
                    //获取方法对象
                    Method getMethod = classType.getMethod(getMethodName, new Class[]{});
                    Method setMethod = classType2.getMethod(setMethodName, new Class[]{field.getType()});//注意set方法需要传入参数类型
                    
                    //调用get方法获取旧的对象的值
                    Object value = getMethod.invoke(object, new Object[]{});
                    if(!filteNullValue || (filteNullValue && value != null)){
                          //调用set方法将这个值复制到新的对象中去
                            setMethod.invoke(copyObject, new Object[]{value});
                    }
                }catch(Exception e){
                    //没有getset方法的属性会报错空指针,不影响,不复制该属性
                }
            }
            
            //获得的父类的父类所有成员变量
            Class<?> superSuperClassType2 = superClassType2.getSuperclass();
            if(superSuperClassType2==null){
                return copyObject;
            }
            Field[] superSuperfields = superSuperClassType2.getDeclaredFields();
            for(Field field : superSuperfields){
                //获取成员变量的名字
                String name = field.getName();    //获取成员变量的名字,此处为id,name,age
                //获取get和set方法的名字
                String firstLetter = name.substring(0,1).toUpperCase();    //将属性的首字母转换为大写            
                String getMethodName = "get" + firstLetter + name.substring(1);
                String setMethodName = "set" + firstLetter + name.substring(1);            
                //System.out.println(getMethodName + "," + setMethodName);
                try{
                    //获取方法对象
                    Method getMethod = classType.getMethod(getMethodName, new Class[]{});
                    Method setMethod = classType2.getMethod(setMethodName, new Class[]{field.getType()});//注意set方法需要传入参数类型
                    
                    //调用get方法获取旧的对象的值
                    Object value = getMethod.invoke(object, new Object[]{});
                    if(!filteNullValue || (filteNullValue && value != null)){
                      //调用set方法将这个值复制到新的对象中去
                        setMethod.invoke(copyObject, new Object[]{value});
                    }
                }catch(Exception e){
                    //没有getset方法的属性会报错空指针,不影响,不复制该属性
                    //e.printStackTrace();
                }
            }
            return copyObject;
     }
     /**
         * 对于父类的属性   默认找2层父子关系      专用于copyObject为接口交互的对象,对象的属性的类型和object的属性类型不一定一致,copyObject的属性的类型都为String
         * @param object   提供数据的类对象
         * @param copyObject  需要进行替换的类对象
         * @return
         * @throws Exception
     */
     public static Object copyForJson(Object object,Object copyObject){
         Class<?> classType = object.getClass();
            Class<?> classType2 = copyObject.getClass();
            
            //获得所有成员变量
            Field[] fields = classType2.getDeclaredFields();
            for(Field field : fields){
                //获取成员变量的名字
                String name = field.getName();    //获取成员变量的名字,此处为id,name,age
                //获取get和set方法的名字
                String firstLetter = name.substring(0,1).toUpperCase();    //将属性的首字母转换为大写            
                String getMethodName = "get" + firstLetter + name.substring(1);
                String setMethodName = "set" + firstLetter + name.substring(1);
                String value = "";
                try{
                    Method getMethod = classType.getMethod(getMethodName, new Class[]{});
                    //调用get方法获取旧的对象的值
                    Object v = getMethod.invoke(object, new Object[]{});
                    value = String.valueOf(v==null?"":v);
                }catch(Exception e){
                    //没有get方法的属性会报错空指针
                }
                try{
                    //获取方法对象
                    Method setMethod = classType2.getMethod(setMethodName, new Class[]{field.getType()});//注意set方法需要传入参数类型
                    //调用set方法将这个值复制到新的对象中去
                    setMethod.invoke(copyObject, new Object[]{value});
                }catch(Exception e){
                    //没有getset方法的属性会报错空指针,不影响,不复制该属性
                }
            }
            
            //获得父类所有成员变量
            Class<?> superClassType2 = classType2.getSuperclass();
            if(superClassType2==null){
                return copyObject;
            }
            Field[] superfields = superClassType2.getDeclaredFields();
            for(Field field : superfields){
                //获取成员变量的名字
                String name = field.getName();    //获取成员变量的名字,此处为id,name,age
                //获取get和set方法的名字
                String firstLetter = name.substring(0,1).toUpperCase();    //将属性的首字母转换为大写            
                String getMethodName = "get" + firstLetter + name.substring(1);
                String setMethodName = "set" + firstLetter + name.substring(1);
                String value = "";
                try{
                    Method getMethod = classType.getMethod(getMethodName, new Class[]{});
                    //调用get方法获取旧的对象的值
                    Object v = getMethod.invoke(object, new Object[]{});
                    value = String.valueOf(v==null?"":v);
                }catch(Exception e){
                    //没有get方法的属性会报错空指针
                }
                try{
                    //获取方法对象
                    Method setMethod = classType2.getMethod(setMethodName, new Class[]{field.getType()});//注意set方法需要传入参数类型
                    //调用set方法将这个值复制到新的对象中去
                    setMethod.invoke(copyObject, new Object[]{value});
                }catch(Exception e){
                    //没有getset方法的属性会报错空指针,不影响,不复制该属性
                }
            }
            
            //获得的父类的父类所有成员变量
            Class<?> superSuperClassType2 = superClassType2.getSuperclass();
            if(superSuperClassType2==null){
                return copyObject;
            }
            Field[] superSuperfields = superSuperClassType2.getDeclaredFields();
            for(Field field : superSuperfields){
                //获取成员变量的名字
                String name = field.getName();    //获取成员变量的名字,此处为id,name,age
                //获取get和set方法的名字
                String firstLetter = name.substring(0,1).toUpperCase();    //将属性的首字母转换为大写            
                String getMethodName = "get" + firstLetter + name.substring(1);
                String setMethodName = "set" + firstLetter + name.substring(1);            
                String value = "";
                try{
                    Method getMethod = classType.getMethod(getMethodName, new Class[]{});
                    //调用get方法获取旧的对象的值
                    Object v = getMethod.invoke(object, new Object[]{});
                    value = String.valueOf(v==null?"":v);
                }catch(Exception e){
                    //没有get方法的属性会报错空指针
                }
                try{
                    //获取方法对象
                    Method setMethod = classType2.getMethod(setMethodName, new Class[]{field.getType()});//注意set方法需要传入参数类型
                    //调用set方法将这个值复制到新的对象中去
                    setMethod.invoke(copyObject, new Object[]{value});
                }catch(Exception e){
                    //没有getset方法的属性会报错空指针,不影响,不复制该属性
                }
            }
            return copyObject;
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值