简体   繁体   English

如何自定义Eclipse生成的hashCode()和equals()?

[英]How to customize hashCode() and equals() generated by Eclipse?

It is recommended and sometimes necessary, classes that represent values ( value classes ) to override hashCode() , equals() [and optionally toString() ] methods. 建议(有时是必要的)表示值( 值类 )的来覆盖hashCode()equals() [和可选的toString() ]方法。 The values that these methods return depend on all or subset of the member variables of the class and its super-class. 这些方法返回的值取决于类及其超类的所有成员变量或子集。 To implement them properly you have to know a little bit of theory about hashing and a little bit of algebra and set theory (not too much, and almost everything is explaind in the javadocs for these methods and in Effective Java form Josh Bloch.) 要正确实现它们,你必须要了解一些关于散列和一点代数和集合理论的理论(不要太多,几乎所有的东西都在这些方法的javadocs中解释 ,并且在有效的Java形式Josh Bloch中。)
In most of the cases, the implementation of this methods follow a template, and IDEs (like Eclipse JDT) include tools to generate them. 在大多数情况下,此方法的实现遵循模板,IDE(如Eclipse JDT)包含生成它们的工具。 However, the tool generators, can not make any assumptions, and generate these methods using only constructs available in the language and the standard library. 但是,工具生成器无法做出任何假设,只使用语言和标准库中提供的构造生成这些方法。 Because of that these methods usually look very ugly. 因为这些方法通常看起来非常难看。

Another way to implement these methods is to use library like Apache's (commons-lang) HashCodeBuilder , EqualsBuilder and ToStringBuilder . 实现这些方法的另一种方法是使用像Apache(commons-lang) HashCodeBuilderEqualsBuilderToStringBuilder Using these utilities, one can implement their own hashCode() and equals() methods that look much better. 使用这些实用程序,可以实现自己看起来更好的hashCode()equals()方法。

My question is about combining these two approaches. 我的问题是关于这两种方法的结合。 I would like to be able to customize Eclipse's hashCode() and equals() generators, so that will generate them using HashCodeBuilder and friends. 我希望能够自定义Eclipse的hashCode()equals()生成器,以便使用HashCodeBuilder和朋友生成它们。 Is it possible (and how) to do this without tweaking the JDT? 如果不调整JDT,是否可以(以及如何)这样做? Only writing small plugin that will override the default implementations (but without changing JDT code). 只编写将覆盖默认实现的小插件(但不更改JDT代码)。

Thanks. 谢谢。

Posting my comment as an answer by request: Commonclipse , an Eclipse plugin that facilitates the use of Apache Commons, does what you want to do. 根据请求发布我的评论作为答案: Commonclipse ,一个便于使用Apache Commons的Eclipse插件,可以做你想做的事情。

Caveat: I have no recent experience with this plugin, which is why I originally posted as a comment, and not as an answer. 警告:我最近没有这个插件的经验,这就是为什么我最初发布的评论,而不是答案。

In the eclipse preferences (Window>Preferences) go to Java > Editor > Templates. 在eclipse首选项(Window> Preferences)中,转到Java> Editor> Templates。

In there you can create a teplate with name:hashcode context:java description:Create a hashcode method. 在那里,您可以创建一个名称为teplate的teplate:hashcode context:java description:创建一个哈希码方法。 The Pattern should contain something like this: 模式应包含以下内容:

public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
}

Save and return to your java class. 保存并返回到您的java类。 Type the name (hashcode) and press ctrl enter. 输入名称(哈希码),然后按ctrl enter。 You can then select your template from the drop down list. 然后,您可以从下拉列表中选择模板。

Do the same for each method you want. 对每种方法都做同样的事情。 You could also create a template that combines everything together as well. 您还可以创建一个将所有内容组合在一起的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM