2009-06-05 342 views
1

我正在使用BeanUtils.copyProperties將一個對象的全部內容複製到另一個對象中,並從中繼承它。BeanUtils.copyProperties缺少深層嵌套變量?

這裏是上下文,從中複製值的域對象包含一組自定義類型爲Xref的對象。該自定義類型具有包含各種類類型的各種字段的嵌入類。

由於某種原因,封裝在嵌入對象內的對象中的一個字段不會被複制。但是我需要的大多數其他東西都會被複制過來。

一個例子:

class Source { 
private Set<Xref> xref; 
... 
} 

class Xref { 
... 
public static class primaryKey { 
... 
private MyObj obj; 
} 
} 

class MyObj { 
private Integer id; 
... 
} 

使用這些名字,如果我嘗試使用BeanUtils.copyProperties到「源」對象的內容複製到「SourceExtended」對象source.xrefs.get的價值(0).getPrimaryKey()。getObj()。getId()不會被複制過來。 在原始對象中它有一個值,但在目標對象中它是空的...

任何想法爲什麼?

謝謝。

回答

7

Javadocs

注意,此方法的目的是執行「淺複製」的屬性和如此複雜的屬性(例如,嵌套的)將不被複制。

+0

明白了!那麼我會解決這個問題。謝謝。 – Lancelot 2009-06-05 16:11:25

3

下面是我用Spring處理這個問題。可能會有所幫助。我的方法是Spring的shallowCopyFieldState的副本,但允許使用字段過濾器。忽略靜態和決賽。

我的方法

public static void shallowCopyFieldState(final Object src, final Object dest, final FieldFilter filter) 
     throws IllegalArgumentException { 
    if (src == null) { 
     throw new IllegalArgumentException("Source for field copy cannot be null"); 
    } 
    if (dest == null) { 
     throw new IllegalArgumentException("Destination for field copy cannot be null"); 
    } 
    if (!src.getClass().isAssignableFrom(dest.getClass())) { 
     throw new IllegalArgumentException("Destination class [" + dest.getClass().getName() 
       + "] must be same or subclass as source class [" + src.getClass().getName() + "]"); 
    } 
    org.springframework.util.ReflectionUtils.doWithFields(src.getClass(), 
      new org.springframework.util.ReflectionUtils.FieldCallback() { 
       public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { 
        org.springframework.util.ReflectionUtils.makeAccessible(field); 
        final Object srcValue = field.get(src); 
        field.set(dest, srcValue); 
       } 
      }, filter); 
} 

Spring的doWithFields:

/** 
* Invoke the given callback on all fields in the target class, 
* going up the class hierarchy to get all declared fields. 
* @param targetClass the target class to analyze 
* @param fc the callback to invoke for each field 
* @param ff the filter that determines the fields to apply the callback to 
*/ 
public static void doWithFields(Class targetClass, FieldCallback fc, FieldFilter ff) 
     throws IllegalArgumentException { 

    // Keep backing up the inheritance hierarchy. 
    do { 
     // Copy each field declared on this class unless it's static or file. 
     Field[] fields = targetClass.getDeclaredFields(); 
     for (int i = 0; i < fields.length; i++) { 
      // Skip static and final fields. 
      if (ff != null && !ff.matches(fields[i])) { 
       continue; 
      } 
      try { 
       fc.doWith(fields[i]); 
      } 
      catch (IllegalAccessException ex) { 
       throw new IllegalStateException(
         "Shouldn't be illegal to access field '" + fields[i].getName() + "': " + ex); 
      } 
     } 
     targetClass = targetClass.getSuperclass(); 
    } 
    while (targetClass != null && targetClass != Object.class); 
}