生成等于使用注释/哈希code /的toString [英] Generating equals / hashcode / toString using annotation
问题描述
我相信我通过确定哪些领域应该是散/平等测试的一部分读的地方(使用APT)的人产生在编译时间等于/哈希code /的toString方法。我找不到像在网络上的任何东西(我可能是在做梦?)...
I believe I read somewhere people generating equals / hashcode / toString methods during compile time (using APT) by identifying which fields should be part of the hash / equality test. I couldn't find anything like that on the web (I might have dreamed it ?) ...
这可以这样做:
public class Person {
@Id @GeneratedValue private Integer id;
@Identity private String firstName, lastName;
@Identity private Date dateOfBirth;
//...
}
有关实体(所以我们要exlude某些领域,如ID)。
For an entity (so we want to exlude some fields, like the id).
或者像斯卡拉案例类即值对象:
Or like a scala case class i.e a value object :
@ValueObject
public class Color {
private int red, green, blue;
}
不仅是文件变得更容易阅读和书写,而且还有助于确保所有的属性都等于/哈希code的一部分(如果你添加另一个属性以后,没有相应地更新方法)。
Not only the file becomes more readable and easier to write, but it also helps ensuring that all the attributes are part of the equals / hashcode (in case you add another attribute later on, without updating the methods accordingly).
我听说APT是不能很好地支持IDE,但我不认为这是一个重大问题。毕竟,测试主要是由持续集成服务器上运行。
I heard APT isn't very well supported in IDE but I wouldn't see that as a major issue. After all, tests are mainly run by continuous integration servers.
如果这个任何想法已经做了,如果没有,为什么?
谢谢
Any idea if this has been done already and if not why ? Thanks
推荐答案
我使用项目龙目岛这一点。
这篇关于生成等于使用注释/哈希code /的toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!