FieldMapper { public static <T> void copyNonNulls(final T source, final T dest, final UpdateAction updateAction) { copy(source, dest, null, updateAction, CopyOption.DONT_COPY_NULLS); }  static void copy(final T source, final T dest); static void copy(final T source, final T dest, final UpdateAction updateAction); static void copyNonNulls(final T source,
                                        final T dest,
                                        final UpdateAction updateAction); static void copyNonNulls(final T source,
                                        final T dest); static void copyNonDefaults(final T source,
                                           final T dest,
                                           final T vanillaObject,
                                           final UpdateAction updateAction); static void copyNonDefaults(final T source,
                                           final T dest,
                                           final T vanillaObject);  }
@Test void testNullSourceNoCopy() { MyObject original = new MyObject(); original.setString(null); MyObject copy = new MyObject(); copy.setString("copy"); FieldMapper.copyNonNulls(original, copy); assertThat(copy.getString()) .isEqualTo("copy"); assertThat(System.identityHashCode(original)) .isNotEqualTo(System.identityHashCode(copy)); }
@Test void testDeepCopyDontCopyNulls() throws NoSuchFieldException, IllegalAccessException { final MyParent parent1 = new MyParent(); System.out.println("parent1 " + System.identityHashCode(parent1)); System.out.println("parent1 child " + System.identityHashCode(parent1.child)); int parent1Id = System.identityHashCode(parent1); int parent1ChildId = System.identityHashCode(parent1.child); final MyParent parent2 = new MyParent(); parent2.setMyInt(99); parent2.setMyString("changed"); parent2.setChild(null); assertThat(parent1) .isNotEqualTo(parent2); assertThat(parent1.getChild()) .isNotEqualTo(parent2.getChild()); System.out.println("parent2 " + System.identityHashCode(parent2)); System.out.println("parent2 child " + System.identityHashCode(parent2.child)); assertThat(System.identityHashCode(parent1)) .isNotEqualTo(System.identityHashCode(parent2)); System.out.println("parent1: " + parent1); System.out.println("parent2: " + parent2); FieldMapper.copyNonNulls(parent2, parent1); System.out.println("parent1: " + parent1); System.out.println("parent2: " + parent2); System.out.println(); System.out.println("parent1 " + System.identityHashCode(parent1)); System.out.println("parent1 child " + System.identityHashCode(parent1.child)); System.out.println("parent2 " + System.identityHashCode(parent2)); System.out.println("parent2 child " + System.identityHashCode(parent2.child)); assertThat(System.identityHashCode(parent1)) .isNotEqualTo(System.identityHashCode(parent2)); assertThat(System.identityHashCode(parent1.child)) .isNotEqualTo(System.identityHashCode(parent2.child)); assertThat(parent1.myInt) .isEqualTo(parent2.myInt); assertThat(parent1.myString) .isEqualTo(parent2.myString); assertThat(parent1.getChild()) .isEqualTo(new MyChild()); assertThat(System.identityHashCode(parent1)) .isEqualTo(parent1Id); assertThat(System.identityHashCode(parent1.child)) .isEqualTo(parent1ChildId); }