使用注释生成equals/hashcode/toString

Bru*_*eth 8 java apt annotations equals hashcode

我相信我通过识别哪些字段应该是散列/相等测试的一部分来阅读在编译期间(使用APT)生成equals/hashcode/toString方法的人.我在网上找不到那样的东西(我可能梦见过它?)......

这可以这样做:

public class Person {
  @Id @GeneratedValue private Integer id;

  @Identity private String firstName, lastName;
  @Identity private Date dateOfBirth;

  //...
}
Run Code Online (Sandbox Code Playgroud)

对于一个实体(所以我们想要排除一些字段,比如id).

或者像scala案例类,即值对象:

@ValueObject
public class Color {
  private int red, green, blue;
}
Run Code Online (Sandbox Code Playgroud)

不仅文件变得更易读和更容易编写,而且它还有助于确保所有属性都是equals/hashcode的一部分(如果您稍后添加其他属性,而不相应地更新方法).

我听说APT在IDE中得不到很好的支持,但我不认为这是一个主要问题.毕竟,测试主要由持续集成服务器运行.

有没有想过这是否已经完成,如果不是为什么?谢谢

Yur*_*kin 16

我正在使用Project Lombok.