2009-06-05 23 views
1

私はBeanUtils.copyPropertiesを使用して、あるオブジェクトのコンテンツ全体をそれを継承する別のオブジェクトにコピーします。BeanUtils.copyPropertiesに深くネストされた変数がありませんか?

ここでは、値がコピーされるドメインオブジェクトには、カスタムタイプXrefのオブジェクトのセットが含まれています。そのカスタムタイプには、さまざまなクラスタイプのさまざまなフィールドを持つ組み込みクラスがあります。

埋め込みオブジェクト内にカプセル化されたオブジェクトのフィールドの1つが何らかの理由でコピーされません。しかし、私が必要とするほとんどのものはコピーされます。一例では

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

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

class MyObj { 
private Integer id; 
... 
} 

私は「SourceExtended」に「ソース」オブジェクトの内容をコピーするBeanUtils.copyPropertiesを使用しようとした場合、これらの名前を使用するには、source.xrefs.getの値をオブジェクト(0).getPrimaryKey()。getObj()。getId()はコピーされません。 元のオブジェクトには値がありますが、ターゲットオブジェクトにはヌルです...

何か考えてみましょうか???

ありがとうございます。 Javadocsから

答えて

7

:このメソッドは、プロパティの「浅いコピー」を実行することが意図されているので、複雑な特性(例えば、ネストされたもの)はコピーされないこと

注意。

+0

がそれを手に入れました!私はそれを回避します。ありがとうございました。 – Lancelot

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); 
} 
関連する問題