java利用反射交換兩個對象中的字段相同的字段值

有時候咱們的兩個對象字段都是同樣的,只有極少的區別,想要把一個對象字段的值,賦值給另一個對象值測試

而後傳給另一個方法使用,可是這個字段太多,一個一個的複製太過繁瑣。this

這時候利用反射解決這個問題。spa

class TestObject1{
    private Integer age;
    private String name;
    //特有的字段
    private String weight;
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getWeight() {
        return weight;
    }
    public void setWeight(String weight) {
        this.weight = weight;
    }
    
}
class TestObject2{
    private Integer age;
    private String name;
    //特有的字段
    private String address;
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
}

編寫test測試類code

public class Test {
        
    /**
     * origin字段向target轉化
     * @param origin
     * @param target
     * @return
     */
    public static <T> T swap(Object origin,T target){
        Field[] tFs = target.getClass().getDeclaredFields();//得到屬性
        Field[] oFs = origin.getClass().getDeclaredFields();//得到屬性
        for(int i=0;i<tFs.length;i++){
            String tname=tFs[i].getName();
            for(int j=0;j<oFs.length;j++){
                if(oFs[j].getName().equals(tname)){
                    Object val=getValue(origin, tname);
                    setValue(target, tname, val);
                }
            }
        }
        return target;
    }
    /**
     * 
     * @param beanObj 實體類
     */
    private static Object getValue(Object beanObj,String name){
        try {
            Field[] fields = beanObj.getClass().getDeclaredFields();//得到屬性
            Class clazz = beanObj.getClass();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                String names=field.getName();
                if(!names.equals(name)){
                    continue;
                }
                // 此處應該判斷beanObj,property不爲null
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method getMethod = pd.getReadMethod();
                if (getMethod != null) {
                    //System.out.println(beanObj+"的字段是:"+field.getName()+",類型是:"+field.getType()+",取到的值是: "+getMethod.invoke(beanObj)); 
                    return getMethod.invoke(beanObj);
                }
            }
        } catch (Exception e) {
             e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     * 
     * @param beanObj 實體類
     * @param name 字段名
     * @param value 值
     */
    private static void setValue(Object beanObj,String name,Object value){
        try {
            Field[] fields = beanObj.getClass().getDeclaredFields();//得到屬性
            Class clazz = beanObj.getClass();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                String names=field.getName();
                if(!names.equals(name)){
                    continue;
                }
                
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
                Method setMethod = pd.getWriteMethod();
                if (setMethod != null) {
                    setMethod.invoke(beanObj, value);
                }
            }
        } catch (Exception e) {
             e.printStackTrace();
        } 
    }
    
}

 

 

 

編寫main方法對象

 public static void main(String[] args) {
    TestObject1 test1=new TestObject1();
    test1.setAge(22);
    test1.setName("gys");
    TestObject2 test2=swap(test1,new TestObject2());
    System.out.println(test2.getName());
    
        
    }
相關文章
相關標籤/搜索