比较几个javabean属性的最佳方法是什么? [英] What is the best way to compare several javabean properties?

查看:914
本文介绍了比较几个javabean属性的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个对象(同一个类的实例)中的几十个字段,并在存在差异时进行一些记录和更新。元代码看起来像这样:

I need to compare dozens of fields in two objects (instances of the same class), and do some logging and updating in case there are differences. Meta code could look something like this:

if (a.getfield1 != b.getfield1)
  log(a.getfield1 is different than b.getfield1)
  b.field1 = a.field1

if (a.getfield2!= b.getfield2)
  log(a.getfield2 is different than b.getfield2)
  b.field2 = a.field2

...

if (a.getfieldn!= b.getfieldn)
  log(a.getfieldn is different than b.getfieldn)
  b.fieldn = a.fieldn

所有比较的代码非常简洁,我想以某种方式使它更紧凑。如果我有一个方法可以作为参数方法调用setter和getter,并为所有字段调用它,那将是很好的,但不幸的是,这是不可能的java。

The code with all the comparisons is very terse, and I would like to somehow make it more compact. It would be nice if I could have a method which would take as a parameter method calls to setter and getter, and call this for all fields, but unfortunately this is not possible with java.

我提出了三个选项,每个选项都有自己的缺点。

I have come up with three options, each which their own drawbacks.

1。使用反射API查找getter和setter

丑陋,如果字段名称发生变化,可能会导致运行时错误

1. Use reflection API to find out getters and setters
Ugly and could cause run time errors in case names of fields change

2。将字段更改为public并直接操作它们而不使用getter和setter

也很丑,并且会将类的实现暴露给外部世界

2. Change fields to public and manipulate them directly without using getters and setters
Ugly as well and would expose implementation of the class to external world

3。让包含类(实体)进行比较,更新更改的字段并返回日志消息

实体不应参与业务逻辑

3. Have the containing class (entity) do the comparison, update changed fields and return log message
Entity should not take part in business logic

所有字段都是字符串类型,如果需要,我可以修改拥有字段的类的代码。

All fields are String type, and I can modify code of the class owning the fields if required.

编辑:类中有一些字段不能是比较。

There are some fields in the class which must not be compared.

推荐答案

使用注释

如果您标记需要比较的字段(无论是否它们是私有的,你仍然不会丢失封装,然后获取这些字段并进行比较。它可以如下:

If you mark the fields that you need to compare (no matter if they are private, you still don't lose the encapsulation, and then get those fields and compare them. It could be as follows:

在需要的类中比较:

@ComparableField 
private String field1;

@ComparableField
private String field2;

private String field_nocomparable;

A在外部类中:

public <T> void compare(T t, T t2) throws IllegalArgumentException,
                                          IllegalAccessException {
    Field[] fields = t.getClass().getDeclaredFields();
    if (fields != null) {
        for (Field field : fields) {
            if (field.isAnnotationPresent(ComparableField.class)) {
                field.setAccessible(true);
                if ( (field.get(t)).equals(field.get(t2)) )
                    System.out.println("equals");
                field.setAccessible(false);
            }
        }
    }
}

代码未经过测试,但如果有帮助请告诉我。

The code is not tested, but let me know if helps.

这篇关于比较几个javabean属性的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆