public static List<String> getChangedFields(Object source, Object destination) { final List<String> returnValue = new ArrayList<>(); if (source.getClass().isInstance(destination)) { Class<?> objectType = source.getClass(); List<PropertyInfo> properties = TypeCompat.getProperties(objectType); for (PropertyInfo property : properties) { Object sourceValue = property.getValue(source, null); Object destinationValue = property.getValue(destination, null); if (property.getCanWrite()&& !Objects.equals(sourceValue, destinationValue)) { returnValue.add(property.getName()); } } } return returnValue; } }
private static Map<String, String> getAvailableValues(String message, AuditLogable logable) { Map<String, String> returnValue = new HashMap<>(logable.getCustomValues()); Set<String> attributes = resolvePlaceHolders(message); if (attributes != null && attributes.size() > 0) { TypeCompat.getPropertyValues(logable, attributes, returnValue); } return returnValue; } }
public static List<PropertyInfo> getProperties(Class<?> type) { List<PropertyInfo> returnValue = new ArrayList<>(); try { PropertyDescriptor[] pds = Introspector.getBeanInfo(type).getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { // Class is a bogus property, remove it if (!CLASS.equals(pd.getName())) { returnValue.add(new PropertyInfo(pd)); } } } catch (Exception e) { throw new RuntimeException(e); } return returnValue; }
@Test public void testGetAvailableValues() { setSomeValue("Anakin"); Map<String, String> values = new HashMap<>(); Set<String> properties = Collections.singleton("somevalue"); TypeCompat.getPropertyValues(this, properties, values); assertFalse(properties.isEmpty()); assertEquals("Anakin", values.get("somevalue"), "Standard"); }