小心Lombok生成代码的“坑” 🤡

361 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

Lombok的@Data注解会帮我们实现equals和hashcode方法,但是有继承关系时,Lombok自动生成的方法可能就不是我们期望的了。

我们先来研究一下其实现:定义一个Person类型,包含姓名和身份证两个字段:

@Data
class Person {
    private String name;
    private String identity;

    public Person(String name, String identity) {
        this.name = name;
        this.identity = identity;
    }
}

对于身份证相同、姓名不同的两个Person对象:

Person person1 = new Person("zhuye","001");
Person person2 = new Person("Joseph","001");
log.info("person1.equals(person2) ? {}", person1.equals(person2));

使用equals判等会得到false。如果你希望只要身份证一致就认为是同一个人的话,可以使用@EqualsAndHashCode.Exclude注解来修饰name字段,从equals和hashCode的实现中排除name字段:

@EqualsAndHashCode.Exclude
private String name;

修改后得到true。打开编译后的代码可以看到,Lombok为Person生成的equals方法的实现,确实只包含了identity属性:

public boolean equals(final Object o) {
    if (o == this) {
        return true;
    } else if (!(o instanceof LombokEquealsController.Person)) {
        return false;
    } else {
        LombokEquealsController.Person other = (LombokEquealsController.Person)o;
        if (!other.canEqual(this)) {
            return false;
        } else {
            Object this$identity = this.getIdentity();
            Object other$identity = other.getIdentity();
            if (this$identity == null) {
                if (other$identity != null) {
                    return false;
                }
            } else if (!this$identity.equals(other$identity)) {
                return false;
            }

            return true;
        }
    }
}

但到这里还没完,如果类型之间有继承,Lombok会怎么处理子类的equals和hashCode呢?我们来测试一下,写一个Employee类继承Person,并新定义一个公司属性:

@Data
class Employee extends Person {

    private String company;
    public Employee(String name, String identity, String company) {
        super(name, identity);
        this.company = company;
    }
}

在如下的测试代码中,声明两个Employee实例,它们具有相同的公司名称,但姓名和身份证均不同:

Employee employee1 = new Employee("zhuye","001", "bkjk.com");
Employee employee2 = new Employee("Joseph","002", "bkjk.com");
log.info("employee1.equals(employee2) ? {}", employee1.equals(employee2));	

很遗憾,结果是true,显然是没有考虑父类的属性,而认为这两个员工是同一人,说明@EqualsAndHashCode默认实现没有使用父类属性。

为解决这个问题,我们可以手动设置callSuper开关为true,来覆盖这种默认行为:

@Data
@EqualsAndHashCode(callSuper = true)
class Employee extends Person {

修改后的代码,实现了同时以子类的属性company加上父类中的属性identity,作为equals和hashCode方法的实现条件(实现上其实是调用了父类的equals和hashCode)。