How to customize hashCode() and equals() generated by Eclipse?
Asked Answered
I

2

17

It is recommended and sometimes necessary, classes that represent values (value classes) to override hashCode(), equals() [and optionally toString()] methods. 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.)
In most of the cases, the implementation of this methods follow a template, and IDEs (like Eclipse JDT) include tools to generate them. 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. Using these utilities, one can implement their own hashCode() and equals() methods that look much better.

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. Is it possible (and how) to do this without tweaking the JDT? Only writing small plugin that will override the default implementations (but without changing JDT code).

Thanks.

Insipid answered 6/6, 2011 at 16:38 Comment(1)
you need to write a plugin if memory servers me right, I, myself, dont use the built-in wizzards.Hypodermic
G
5

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.

Caveat: I have no recent experience with this plugin, which is why I originally posted as a comment, and not as an answer.

Gymnasiast answered 6/6, 2011 at 23:29 Comment(0)
C
2

In the eclipse preferences (Window>Preferences) go to Java > Editor > Templates.

In there you can create a teplate with name:hashcode context:java description:Create a hashcode method. The Pattern should contain something like this:

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

Save and return to your java class. Type the name (hashcode) and press 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.

Chameleon answered 6/6, 2011 at 17:1 Comment(3)
I was hoping for something more sophisticated than template. The templates are pretty dumb and you can not generate code that depends on the context, let say, member variables. Reflections is not always an option since it is slow for general puropse. Anyway, i lake the hint. (+1)Insipid
I'd have downvoted this but i do not vote, you do understand that approach is beyond terrible: a) it is pitifully slow for a function that governs the speed of the search b) worse: it can take any field that may or may not be immutable (hence should not participate)Hypodermic
bestss: c) It will fail under a security manager, unless the appropriate permissions are set up correctly. This is just an exapmle of how to create templates. Its quick, easy and built into eclipse.Chameleon

© 2022 - 2024 — McMap. All rights reserved.