类属性拷贝

来源:互联网 发布:牡丹江医学院网络答题 编辑:程序博客网 时间:2024/06/04 19:05
package com.sosgps.dao.util;

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


//类属性复制方法类
public class ClassCopyUtil {
    /**
     * 对于父类的属性   默认找2层父子关系
     * @param object   提供数据的类对象
     * @param copyObject  需要进行替换的类对象
     * @return
     * @throws Exception
     */
     public static Object copy(Object object,Object copyObject) throws Exception{
            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[]{});
                    //调用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[]{});
                    //调用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[]{});
                    //调用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) throws Exception{
         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;
     }
}

0 0
原创粉丝点击