FieldMetadataUtils { @SuppressWarnings("unchecked") public static <T extends FieldMetadata> T diff(T from, T to) throws IllegalStateException { try { T empty = (T) invokeConstructor(from.getClass()); List<PropertyDescriptor> descriptors = getDescriptors(from.getClass()); for(PropertyDescriptor descriptor : descriptors) { diffProperty(empty, to, from, descriptor); } return empty; } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException ex) { throw new IllegalStateException("Failed to diff bean", ex); } } private  FieldMetadataUtils(); @SuppressWarnings("unchecked") static T diff(T from, T to); @SuppressWarnings("unchecked") static T merge(T from, T to); static void updateObject(T from, T to);  }
@Test public void diff() throws InvocationTargetException, IllegalAccessException { StringFieldMetadata custom = new StringFieldMetadata(); List<String> tokens = Arrays.asList("a", "b"); String fullPath = "fString.new"; custom.setFullPath(fullPath); custom.setTokens(tokens); StringFieldMetadata updated = FieldMetadataUtils.diff(custom, MetadataConstants.DEFAULT_STRING_META); LOGGER.info("{}", updated); assertEquals(updated.getFullPath(), fullPath); assertEquals(updated.getTokens(), tokens); assertNull(updated.getTokensPerValue()); assertNull(updated.getValuesPerDocument()); }
@Test public void diffObject() throws InvocationTargetException, IllegalAccessException { ObjectTypeMetadata defaultMeta = MetadataConstants.DEFAULT_OBJECT_META; ObjectTypeMetadata field = new ObjectTypeMetadata("obj", Arrays.asList(MetadataConstants.DEFAULT_STRING_META)); ObjectTypeMetadata diff = FieldMetadataUtils.diff(field, defaultMeta); assertEquals(diff.getInnerMetadata().size(), 1); }