@Test public void testGetModifiedFields() throws Exception { TestBean cg0 = new TestBean(); TestBean cg1 = new TestBean(); cg1.setAlias("aaa:1.0.0"); cg1.setHeartbeat(2222); cg1.setRegister(true); TestBean cg2 = new TestBean(); cg2.setAlias("aaa:1.0.0"); cg2.setHeartbeat(2222); cg2.setRegister(false); cg2.setSubBeans(new ArrayList<TestSubBean>()); Assert.assertTrue(BeanUtils.getModifiedFields(cg0, cg1).size() == 3); Assert.assertTrue(BeanUtils.getModifiedFields(cg0, cg2).size() == 3); Assert.assertTrue(BeanUtils.getModifiedFields(cg1, cg2).size() == 2); }
public static <T> List<String> getModifiedFields(T src, T dst, String... ignoreFields) { Class clazz = src.getClass(); Method[] methods = clazz.getMethods(); List<String> ignoreFiledList = Arrays.asList(ignoreFields); List<String> modifiedFields = new ArrayList<String>(); for (Method getterMethod : methods) { if (Modifier.isStatic(getterMethod.getModifiers()) || !ReflectUtils.isBeanPropertyReadMethod(getterMethod)) { continue; } String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(getterMethod); if (ignoreFiledList.contains(propertyName)) { continue; } Class returnType = getterMethod.getReturnType(); try { Method setterMethod = ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnType); if (setterMethod != null) { Object srcVal = getterMethod.invoke(src); Object dstVal = getterMethod.invoke(dst); if (srcVal == null) { if (dstVal != null) { modifiedFields.add(propertyName); } } else { if (dstVal == null) { modifiedFields.add(propertyName); } else { if (!srcVal.equals(dstVal)) { modifiedFields.add(propertyName); } } } } } catch (Exception ignore) { } } return modifiedFields; }
BeanUtils { public static <T> List<String> getModifiedFields(T src, T dst, String... ignoreFields) { Class clazz = src.getClass(); Method[] methods = clazz.getMethods(); List<String> ignoreFiledList = Arrays.asList(ignoreFields); List<String> modifiedFields = new ArrayList<String>(); for (Method getterMethod : methods) { if (Modifier.isStatic(getterMethod.getModifiers()) || !ReflectUtils.isBeanPropertyReadMethod(getterMethod)) { continue; } String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(getterMethod); if (ignoreFiledList.contains(propertyName)) { continue; } Class returnType = getterMethod.getReturnType(); try { Method setterMethod = ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnType); if (setterMethod != null) { Object srcVal = getterMethod.invoke(src); Object dstVal = getterMethod.invoke(dst); if (srcVal == null) { if (dstVal != null) { modifiedFields.add(propertyName); } } else { if (dstVal == null) { modifiedFields.add(propertyName); } else { if (!srcVal.equals(dstVal)) { modifiedFields.add(propertyName); } } } } } catch (Exception ignore) { } } return modifiedFields; } }
BeanUtils { public static <T> List<String> getModifiedFields(T src, T dst, String... ignoreFields) { Class clazz = src.getClass(); Method[] methods = clazz.getMethods(); List<String> ignoreFiledList = Arrays.asList(ignoreFields); List<String> modifiedFields = new ArrayList<String>(); for (Method getterMethod : methods) { if (Modifier.isStatic(getterMethod.getModifiers()) || !ReflectUtils.isBeanPropertyReadMethod(getterMethod)) { continue; } String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(getterMethod); if (ignoreFiledList.contains(propertyName)) { continue; } Class returnType = getterMethod.getReturnType(); try { Method setterMethod = ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnType); if (setterMethod != null) { Object srcVal = getterMethod.invoke(src); Object dstVal = getterMethod.invoke(dst); if (srcVal == null) { if (dstVal != null) { modifiedFields.add(propertyName); } } else { if (dstVal == null) { modifiedFields.add(propertyName); } else { if (!srcVal.equals(dstVal)) { modifiedFields.add(propertyName); } } } } } catch (Exception ignore) { } } return modifiedFields; }  }
BeanUtils { public static <T> List<String> getModifiedFields(T src, T dst, String... ignoreFields) { Class clazz = src.getClass(); Method[] methods = clazz.getMethods(); List<String> ignoreFiledList = Arrays.asList(ignoreFields); List<String> modifiedFields = new ArrayList<String>(); for (Method getterMethod : methods) { if (Modifier.isStatic(getterMethod.getModifiers()) || !ReflectUtils.isBeanPropertyReadMethod(getterMethod)) { continue; } String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(getterMethod); if (ignoreFiledList.contains(propertyName)) { continue; } Class returnType = getterMethod.getReturnType(); try { Method setterMethod = ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnType); if (setterMethod != null) { Object srcVal = getterMethod.invoke(src); Object dstVal = getterMethod.invoke(dst); if (srcVal == null) { if (dstVal != null) { modifiedFields.add(propertyName); } } else { if (dstVal == null) { modifiedFields.add(propertyName); } else { if (!srcVal.equals(dstVal)) { modifiedFields.add(propertyName); } } } } } catch (Exception ignore) { } } return modifiedFields; }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields); }
BeanUtils { public static <T> List<String> getModifiedFields(T src, T dst, String... ignoreFields) { Class clazz = src.getClass(); Method[] methods = clazz.getMethods(); List<String> ignoreFiledList = Arrays.asList(ignoreFields); List<String> modifiedFields = new ArrayList<String>(); for (Method getterMethod : methods) { if (Modifier.isStatic(getterMethod.getModifiers()) || !ReflectUtils.isBeanPropertyReadMethod(getterMethod)) { continue; } String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(getterMethod); if (ignoreFiledList.contains(propertyName)) { continue; } Class returnType = getterMethod.getReturnType(); try { Method setterMethod = ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnType); if (setterMethod != null) { Object srcVal = getterMethod.invoke(src); Object dstVal = getterMethod.invoke(dst); if (srcVal == null) { if (dstVal != null) { modifiedFields.add(propertyName); } } else { if (dstVal == null) { modifiedFields.add(propertyName); } else { if (!srcVal.equals(dstVal)) { modifiedFields.add(propertyName); } } } } } catch (Exception ignore) { } } return modifiedFields; }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields);  }