如何比较两个对象并获取更改的字段

时间:2019-03-22 06:58:01

标签: java object-comparison

在此处记录对特定对象记录所做的更改。因此,即时比较旧记录和更新的记录,以字符串形式记录更新的字段。知道我该怎么做吗?

3 个答案:

答案 0 :(得分:0)

  1. 也许这种方法可以帮助您解决问题
     /**
     * get o1 and o2 different value of field name
     * @param o1 source
     * @param o2 target
     * @return
     * @throws IllegalAccessException
     */
    public static List<String> getDiffName(Object o1,Object o2) throws IllegalAccessException {
        //require o1 and o2 is not null
        if (o1==null&&o2==null){
            return Collections.emptyList();
        }
        //if only one has null
        if (o1 == null){
            return getAllFiledName(o2);
        }
        if (o2 == null){
            return getAllFiledName(o1);
        }
        //source field
        Field[] fields=o1.getClass().getDeclaredFields();
        List<String> fieldList=new ArrayList<>(fields.length);
        //if class is same using this to call
        if (o1.getClass().equals(o2.getClass())){
            //loop field to equals the field
            for (Field field : fields) {
                //to set the field access
                field.setAccessible(true);
                Object source = field.get(o1);
                Object target = field.get(o2);
                //using jdk8 equals to compare two objects
                if (!Objects.equals(source, target)){
                    fieldList.add(field.getName());
                }
            }
        }else {
            //maybe o1 class is not same as o2 class
            Field[] targetFields=o2.getClass().getDeclaredFields();
            List<String> sameFieldNameList=new ArrayList<>();
            //loop o1 field
            for (Field field : fields) {
                String name = field.getName();
                //loop target field to get same field
                for (Field targetField : targetFields) {
                    //if name is equal to compare
                    if (targetField.getName().equals(name)){
                        //add same field to list
                        sameFieldNameList.add(name);
                        //set access
                        field.setAccessible(true);
                        Object source = field.get(o1);
                        //set target access
                        targetField.setAccessible(true);
                        Object target = targetField.get(o2);
                        //equals
                        if (!Objects.equals(source, target)){
                            fieldList.add(field.getName());
                        }
                    }
                }
            }
            //after loop add different source
            for (Field targetField : targetFields) {
                //add not same field
                if (!sameFieldNameList.contains(targetField.getName())){
                    fieldList.add(targetField.getName());
                }
            }
        }
        return fieldList;
    }

    /**
     * getAllFiledName
     * @param obj
     * @return
     */
    private static List<String> getAllFiledName(Object obj) {
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        List<String> list=new ArrayList<>(declaredFields.length);
        for (Field field : declaredFields) {
            list.add(field.getName());
        }
        return list;
    }
  1. 此方法可以比较两个具有相同名称字段的对象,如果它们没有相同的字段,则返回所有字段Name

答案 1 :(得分:0)

我找到了如下解决方案:

Type

答案 2 :(得分:0)

您可以为此使用javers库。

  <groupId>org.javers</groupId>
  <artifactId>javers-core</artifactId>

POJO:

    public class Person {
    private Integer id;
    private String name;

    // standard getters/constructors
}

用法:

    @Test
public void givenPersonObject_whenApplyModificationOnIt_thenShouldDetectChange() {
    // given
    Javers javers = JaversBuilder.javers().build();

    Person person = new Person(1, "Michael Program");
    Person personAfterModification = new Person(1, "Michael Java");

    // when
    Diff diff = javers.compare(person, personAfterModification);

    // then
    ValueChange change = diff.getChangesByType(ValueChange.class).get(0);

    assertThat(diff.getChanges()).hasSize(1);
    assertThat(change.getPropertyName()).isEqualTo("name");
    assertThat(change.getLeft()).isEqualTo("Michael Program");
    assertThat(change.getRight()).isEqualTo("Michael Java");
}

还支持其他用例。