41

Some days ago we switched to Java 7 within my Company - finally! Jay \o/ So I found out about the Objects class and was astonished how short the methods hashCode() and equals() were realized, reducing a lot of boylerplate code compared to the ones generated by eclipse per default (ALT+SHIFT+S --> H).

I was wondering if I could change the default behaviour of the eclipse generated hashCode() and equals()?

I'd love to see this:

@Override
public int hashCode()
{
  return Objects.hash(one, two, three, four/*, ...*/);
}

instead of this:

@Override
public int hashCode()
{
  final int prime = 31;
  int result = 1;
  result = prime * result + ((one == null) ? 0 : one.hashCode());
  result = prime * result + ((two == null) ? 0 : two.hashCode());
  result = prime * result + ((three == null) ? 0 : three.hashCode());
  result = prime * result + ((four== null) ? 0 : four.hashCode());
  // ...
  return result;
}

The same goes for equals(). This is the article I got this from.

Any ideas how to realize this best?

Aufwind
  • 25,310
  • 38
  • 109
  • 154
  • For "Generate toString()" it is possible to define a template on the wizzard page. You should file a ticket at [eclipse-bugzilla](https://bugs.eclipse.org/bugs/) maybe they add something similar for eq+hashcode in the future. If you do so please a like to the ticket, so everyone interested can upvote it! – Chriss Jul 22 '13 at 07:49
  • You can just as well fold the `hashCode()` function if you don't want to see it. I think it might be a good idea to consider whether or not the default implementation is what you want in all possible cases in the future. By defaulting to the shorthand expression you would be forgoing your flexibility in tweaking the `hashCode` (not irreversible of course), should you want/need to do so. See http://stackoverflow.com/questions/12076846/using-a-larger-prime-as-a-multiplier-when-overriding-hashcode for a discussion on hash functions. – posdef Jul 22 '13 at 08:01
  • 1
    I've logged this enhancement. Vote for it! https://bugs.eclipse.org/bugs/show_bug.cgi?id=424214 – Nicolas C Dec 17 '13 at 09:57
  • 2 years later and still nothing has changed. Not even one comment on the reported bug. The best solution is to just use IntelliJ. – Doron Gold Jul 20 '15 at 10:57
  • You can also use Builder from apache commons: http://stackoverflow.com/a/22411032/644450 – oers Jun 07 '16 at 14:02

3 Answers3

19

hashCode and equals generation using the Java 7 Objects class has now been implemented in Eclipse. I was working on the feature request 424214 back in August 2018 and my contributions were merged in the JDT UI codebase shortly afterwards (see commit f543cd6).

Here's an overview of the new option in the Source > Generate hashCode() and equals... tool:

New option in generation tool

This has been officially released in Eclipse 4.9 in September 2018. Simply download the latest version of Eclipse (downloads can be found here), or install the latest available software with the following update site: http://download.eclipse.org/releases/latest

In addition to this new feature, arrays are now handled more cleverly. The generation will use the Arrays.deepHashCode and Arrays.deepEquals methods in a number of cases where it would previously incorrectly prefer the standard Arrays.hashCode and Arrays.equals alternatives.

Pyves
  • 6,333
  • 7
  • 41
  • 59
4

In the Eclipse preferences go to Java > Editor > Templates.

In there you can create a new template. The pattern could look like:

@Override
public int hashCode()
{
     return Objects.hash(one, two, three, four/*, ...*/);
}

I'm not sure if there's a variable that will properly enumerate your fields however.

You might want to look at some further explanations on these templates

reto
  • 9,995
  • 5
  • 53
  • 52
  • 8
    Unfortunately the template system does not support enumerating the fields (something similar to **${enclosing_method_arguments}**, it could have been _${enclosing_type_fields}_ if it existed). Even if you write a template to automatically generate the boilerplate, you will still have to manually fill in the field names inside the `hash()` method. And I have no idea why the hashCode and equals template is not listed in _Java > Code Style > Code Templates_... – ADTC Jul 22 '13 at 10:04
3

There is a new plugin available which can generate toString(), hashCode(), equals() methods using java 7 features, apache common lang library, guava library. It has good customizable features. Please find the link below to install plugin. After installation, just right click -> Jenerate -> different options

The link - https://marketplace.eclipse.org/content/jenerate

  • Unfortunately, this plugin has a bug, it does not generate the hashCode and equals correctly if you have a super class. – Jack Zhang Apr 01 '17 at 22:43