@Test public void shouldCombineSynteticObjects() throws Exception { Person captain = new Person("Jim", null, null); Person member1 = new Person("Joe", null, null); Team team1 = new Team("A", captain, null); List<Person> members = new ArrayList<>(); members.add(member1); Team team2 = new Team("A", null, members); Team combined = Beans.combine(team1, team2); assertNotNull(combined); assertEquals("A", combined.getName()); assertEquals(captain, combined.getCaptain()); assertNotNull(combined.getMembers()); assertEquals(member1, combined.getMembers().get(0)); }
public static <C> C combine(C origin, C override) { if (origin == null) { return override; } if (override == null) { return origin; } Class<C> originClass = (Class<C>) origin.getClass(); Class<C> overrideClass = (Class<C>) override.getClass(); if (String.class.isAssignableFrom(originClass)) { if (Strings.isNullOrEmpty((String) origin)) { return override; } if (Strings.isNullOrEmpty((String) override)) { return origin; } } if (!overrideClass.equals(originClass)) { throw new IllegalStateException( String.format("Objects types don't match. Found: [%s] and [%s].", overrideClass, originClass)); } if (overrideClass.isPrimitive() || overrideClass.isEnum()) { return override; } if (List.class.isAssignableFrom(originClass)) { return mergeList(origin, override); } if (Set.class.isAssignableFrom(originClass)) { return mergeSet(origin, override); } if (Map.class.isAssignableFrom(originClass)) { return mergeMap(origin, override); } if (originClass.isArray()) { return mergeArray(origin, override); } String fcqn = overrideClass.getCanonicalName(); if (fcqn.startsWith("java") || fcqn.startsWith("com.sun") || fcqn.startsWith("sun.")) { return override; } try { final C result = originClass.newInstance(); for (Field f : getAllFields(originClass)) { try { String name = f.getName(); f.setAccessible(true); Object value = combine(f.get(origin), f.get(override)); f.set(result, value); } catch (Exception e) { throw DekorateException.launderThrowable(e); } } return result; } catch (Exception e) { throw DekorateException.launderThrowable(e); } }
Beans { public static <C> C combine(C origin, C override) { if (origin == null) { return override; } if (override == null) { return origin; } Class<C> originClass = (Class<C>) origin.getClass(); Class<C> overrideClass = (Class<C>) override.getClass(); if (String.class.isAssignableFrom(originClass)) { if (Strings.isNullOrEmpty((String) origin)) { return override; } if (Strings.isNullOrEmpty((String) override)) { return origin; } } if (!overrideClass.equals(originClass)) { throw new IllegalStateException( String.format("Objects types don't match. Found: [%s] and [%s].", overrideClass, originClass)); } if (overrideClass.isPrimitive() || overrideClass.isEnum()) { return override; } if (List.class.isAssignableFrom(originClass)) { return mergeList(origin, override); } if (Set.class.isAssignableFrom(originClass)) { return mergeSet(origin, override); } if (Map.class.isAssignableFrom(originClass)) { return mergeMap(origin, override); } if (originClass.isArray()) { return mergeArray(origin, override); } String fcqn = overrideClass.getCanonicalName(); if (fcqn.startsWith("java") || fcqn.startsWith("com.sun") || fcqn.startsWith("sun.")) { return override; } try { final C result = originClass.newInstance(); for (Field f : getAllFields(originClass)) { try { String name = f.getName(); f.setAccessible(true); Object value = combine(f.get(origin), f.get(override)); f.set(result, value); } catch (Exception e) { throw DekorateException.launderThrowable(e); } } return result; } catch (Exception e) { throw DekorateException.launderThrowable(e); } } }
Beans { public static <C> C combine(C origin, C override) { if (origin == null) { return override; } if (override == null) { return origin; } Class<C> originClass = (Class<C>) origin.getClass(); Class<C> overrideClass = (Class<C>) override.getClass(); if (String.class.isAssignableFrom(originClass)) { if (Strings.isNullOrEmpty((String) origin)) { return override; } if (Strings.isNullOrEmpty((String) override)) { return origin; } } if (!overrideClass.equals(originClass)) { throw new IllegalStateException( String.format("Objects types don't match. Found: [%s] and [%s].", overrideClass, originClass)); } if (overrideClass.isPrimitive() || overrideClass.isEnum()) { return override; } if (List.class.isAssignableFrom(originClass)) { return mergeList(origin, override); } if (Set.class.isAssignableFrom(originClass)) { return mergeSet(origin, override); } if (Map.class.isAssignableFrom(originClass)) { return mergeMap(origin, override); } if (originClass.isArray()) { return mergeArray(origin, override); } String fcqn = overrideClass.getCanonicalName(); if (fcqn.startsWith("java") || fcqn.startsWith("com.sun") || fcqn.startsWith("sun.")) { return override; } try { final C result = originClass.newInstance(); for (Field f : getAllFields(originClass)) { try { String name = f.getName(); f.setAccessible(true); Object value = combine(f.get(origin), f.get(override)); f.set(result, value); } catch (Exception e) { throw DekorateException.launderThrowable(e); } } return result; } catch (Exception e) { throw DekorateException.launderThrowable(e); } }  }
Beans { public static <C> C combine(C origin, C override) { if (origin == null) { return override; } if (override == null) { return origin; } Class<C> originClass = (Class<C>) origin.getClass(); Class<C> overrideClass = (Class<C>) override.getClass(); if (String.class.isAssignableFrom(originClass)) { if (Strings.isNullOrEmpty((String) origin)) { return override; } if (Strings.isNullOrEmpty((String) override)) { return origin; } } if (!overrideClass.equals(originClass)) { throw new IllegalStateException( String.format("Objects types don't match. Found: [%s] and [%s].", overrideClass, originClass)); } if (overrideClass.isPrimitive() || overrideClass.isEnum()) { return override; } if (List.class.isAssignableFrom(originClass)) { return mergeList(origin, override); } if (Set.class.isAssignableFrom(originClass)) { return mergeSet(origin, override); } if (Map.class.isAssignableFrom(originClass)) { return mergeMap(origin, override); } if (originClass.isArray()) { return mergeArray(origin, override); } String fcqn = overrideClass.getCanonicalName(); if (fcqn.startsWith("java") || fcqn.startsWith("com.sun") || fcqn.startsWith("sun.")) { return override; } try { final C result = originClass.newInstance(); for (Field f : getAllFields(originClass)) { try { String name = f.getName(); f.setAccessible(true); Object value = combine(f.get(origin), f.get(override)); f.set(result, value); } catch (Exception e) { throw DekorateException.launderThrowable(e); } } return result; } catch (Exception e) { throw DekorateException.launderThrowable(e); } }  static C combine(C origin, C override); }
Beans { public static <C> C combine(C origin, C override) { if (origin == null) { return override; } if (override == null) { return origin; } Class<C> originClass = (Class<C>) origin.getClass(); Class<C> overrideClass = (Class<C>) override.getClass(); if (String.class.isAssignableFrom(originClass)) { if (Strings.isNullOrEmpty((String) origin)) { return override; } if (Strings.isNullOrEmpty((String) override)) { return origin; } } if (!overrideClass.equals(originClass)) { throw new IllegalStateException( String.format("Objects types don't match. Found: [%s] and [%s].", overrideClass, originClass)); } if (overrideClass.isPrimitive() || overrideClass.isEnum()) { return override; } if (List.class.isAssignableFrom(originClass)) { return mergeList(origin, override); } if (Set.class.isAssignableFrom(originClass)) { return mergeSet(origin, override); } if (Map.class.isAssignableFrom(originClass)) { return mergeMap(origin, override); } if (originClass.isArray()) { return mergeArray(origin, override); } String fcqn = overrideClass.getCanonicalName(); if (fcqn.startsWith("java") || fcqn.startsWith("com.sun") || fcqn.startsWith("sun.")) { return override; } try { final C result = originClass.newInstance(); for (Field f : getAllFields(originClass)) { try { String name = f.getName(); f.setAccessible(true); Object value = combine(f.get(origin), f.get(override)); f.set(result, value); } catch (Exception e) { throw DekorateException.launderThrowable(e); } } return result; } catch (Exception e) { throw DekorateException.launderThrowable(e); } }  static C combine(C origin, C override);  }