使用反射将一个对象的值复制给另一个对象(同一个对象或者基于同一个基类)

我写了两个方法:

第一个方法是一个类的不同实例化对象,比如类A,实例化对象a1,a2,:(使用的方法是得到类的属性名)

 

[html]  view plain  copy
 
  1. /**  
  2.  *   
  3.  *<p>  
  4.  *@description 转换javabean ,将class2中的属性值赋值给class1,如果class1属性有值,则不覆盖  
  5.  *</p>  
  6.  *@param class1 基准类,被赋值对象  
  7.  *@param class2 提供数据的对象  
  8.  *@throws Exception  
  9.  * @see  
  10.  */  
  11. private void converJavaBean(Object class1, Object class2) {  
  12.     Class<?> clazz1 = class1.getClass();  
  13.     Class<?> clazz2 = class2.getClass();  
  14.     Field[] fields1 = clazz1.getDeclaredFields();  
  15.     Field[] fields2 = clazz2.getDeclaredFields();  
  16.     for (int i = 0; i < fields1.length; i++) {  
  17.         try {  
  18.             fields1[i].setAccessible(true);  
  19.             fields2[i].setAccessible(true);  
  20.             Object obg1 = fields1[i].get(class1);  
  21.             Object obg2 = fields2[i].get(class2);  
  22.   
  23.             if (null == fields1[i].get(class1) && null != fields2[i].get(class2)) {  
  24.                 fields1[i].set(class1, fields2[i].get(class2));  
  25.             }  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30. }  

第二个方法得到get和set方法进行赋值,比第一个功能强大,只要有相同的属性名都可以

 

 

[java]  view plain  copy
 
  1. /** 
  2.     *  
  3.     * <p> 
  4.     *  
  5.     * @description 转换javabean ,将class2中的属性值赋值给class1,如果class1属性有值,则不覆盖 
  6.     *              ,前提条件是有相同的属性名 
  7.     *              </p> 
  8.     * @param class1 
  9.     *            基准类,被赋值对象 
  10.     * @param class2 
  11.     *            提供数据的对象 
  12.     * @throws Exception 
  13.     * @author ex_dingyongbiao 
  14.     * @see 
  15.     */  
  16.    public static void converJavaBean(Object class1, Object class2) {  
  17.        try {  
  18.            Class<?> clazz1 = class1.getClass();  
  19.            Class<?> clazz2 = class2.getClass();  
  20.            // 得到method方法  
  21.            Method[] method1 = clazz1.getMethods();  
  22.            Method[] method2 = clazz2.getMethods();  
  23.   
  24.            int length1 = method1.length;  
  25.            int length2 = method2.length;  
  26.            if (length1 != 0 && length2 != 0) {  
  27.                // 创建一个get方法数组,专门存放class2的get方法。  
  28.                Method[] get = new Method[length2];  
  29.                for (int i = 0, j = 0; i < length2; i++) {  
  30.                    if (method2[i].getName().indexOf("get") == 0) {  
  31.                        get[j] = method2[i];  
  32.                        ++j;  
  33.                    }  
  34.                }  
  35.   
  36.                for (int i = 0; i < get.length; i++) {  
  37.                    if (get[i] == null)// 数组初始化的长度多于get方法,所以数组后面的部分是null  
  38.                        continue;  
  39.                    // 得到get方法的值,判断时候为null,如果为null则进行下一个循环  
  40.                    Object value = get[i].invoke(class2, new Object[] {});  
  41.                    if (null == value)  
  42.                        continue;  
  43.                    // 得到get方法的名称 例如:getXxxx  
  44.                    String getName = get[i].getName();  
  45.                    // 得到set方法的时候传入的参数类型,就是get方法的返回类型  
  46.                    Class<?> paramType = get[i].getReturnType();  
  47.                    Method getMethod = null;  
  48.                    try {  
  49.                        // 判断在class1中时候有class2中的get方法,如果没有则抛异常继续循环  
  50.                        getMethod = clazz1.getMethod(getName, new Class[] {});  
  51.                    } catch (NoSuchMethodException e) {  
  52.                        continue;  
  53.                    }  
  54.                    // class1的get方法不为空并且class1中get方法得到的值为空,进行赋值,如果class1属性原来有值,则跳过  
  55.                    if (null == getMethod || null != getMethod.invoke(class1, new Object[] {}))  
  56.                        continue;  
  57.                    // 通过getName 例如getXxxx 截取后得到Xxxx,然后在前面加上set,就组装成set的方法名  
  58.                    String setName = "set" + getName.substring(3);  
  59.                    // 得到class1的set方法,并调用  
  60.                    Method setMethod = clazz1.getMethod(setName, paramType);  
  61.                    setMethod.invoke(class1, value);  
  62.                }  
  63.            }  
  64.        } catch(Exception e) {  
  65.         System.out.println(e);  
  66.        }  
  67.    }  

推荐使用第二种方法。

猜你喜欢

转载自xxxyzyl.iteye.com/blog/2408828