About auto-generate the equals method

In intellij, while we use alt + insert and choose "equals() and hashcode()"
idea will generate the equals and hashcode method for us.
But I can't change how the equals/hashcode method's generation.
for example :
original code idea generate
if (o == null || getClass() != o.getClass()) return false;

code I want
if (o == null || !(o instanceif $class$)) return false;

The second one is more suitable while we are using something enhanced by cglib and so on.

13 comments
Comment actions Permalink

+1 for equals and hashcode templates (like the tostring plugin offers for tostring)

0
Comment actions Permalink

I see this was posted in February. Has there been any change? Can we configure an equals method template?

0
Comment actions Permalink

Hello Rezrovs,

I see this was posted in February. Has there been any change? Can we
configure an equals method template?


No, the feature hasn't been implemented, and it's not clear how exactly it
should be implemented. There's quite a lot of logic in IntelliJ IDEA's code
for generating equals(), and we'll need either to move all this logic to
a template (which is kinda ugly) or provide many small templates for different
parts of the method which will be combined as needed by the Java code in
the IDE.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks for the reply.

What we did to get around it is added a live template to just give the basic form of the method that we will then flesh out manually.

A suggestion would be to have a number of different types of equals methods included and the user can choose between them. So while the user can't completely modify the equals method, they can at least have an option with the comparison.

For example having this as one of the alternatives
if (!(o instanceof MyClass)) return false;

(This comparison is more useful when using Hibernate than the getClass() != o.getClass() )

And then in the dialog that pops up (during the auto-generate) to ask the user to select the variables to include in the method, they could also choose the way they would like the comparison of the Class to work from the set of alternatives.

0
Comment actions Permalink

2 small templates would be enough. one for comparing 2 fields with 2 variables (this.field, other.field), and one with one variable (the given object) to check for the objects class.

0
Comment actions Permalink

Hello Dmitry,

Hello Rezrovs,

>> I see this was posted in February. Has there been any change? Can we
>> configure an equals method template?
>>

No, the feature hasn't been implemented, and it's not clear how
exactly it should be implemented. There's quite a lot of logic in
IntelliJ IDEA's code for generating equals(), and we'll need either to
move all this logic to a template (which is kinda ugly) or provide
many small templates for different parts of the method which will be
combined as needed by the Java code in the IDE.


In addition I doubt many users would actively customize equals()/hashCode()
templates.
All things considered I would say that the IDEA generated methods are better
than ones written by an 'average' developer.

While I submitted IDEADEV-10999 some time ago, my main motivation actually
was to fix small annoyances in the generated code, not to have a full template
system.
Perhaps putting a number of such fixes in the current code generation would
satisfy most of the request for hashCode()/equals() templates?

Specific wishes from memory (and IDEADEV-10999)
-Generated equals flags "Redundant 'if' statement"
-A comment in IDEADEV-10999 requested generated @Override
-Generated parameters did not respect "final" settings (perhaps they do now?)
-...

Taras


0
Comment actions Permalink

Hello HamsterofDeath,

2 small templates would be enough. one for comparing 2 fields with 2
variables (this.field, other.field)


There are at least five different cases of such comparison (object field,
not-null object field, primitive field, double field and array field).

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hello Rezrovs,

A suggestion would be to have a number of different types of equals
methods included and the user can choose between them. So while the
user can't completely modify the equals method, they can at least have
an option with the comparison.

For example having this as one of the alternatives if (!(o instanceof
MyClass)) return false;

(This comparison is more useful when using Hibernate that the
getClass() != o.getClass() )

And then in the dialog that pops up to ask the user to select the
variables to include in the method, they could also choose the way
they would like the comparison of the Class to work from the set of
alternatives.


If it's the only thing that can be usefully customized (and as far as I can
see it is), then probably the best solution is to add a single configurable
template for the instance class comparison.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

jetbrains: 1
hamster: 0

0
Comment actions Permalink

+1 for generated code. It really grates me when IJ generates code, only to immediately highlight it as code that needs improvement. It is supposed to make my code better, not worse.

0
Comment actions Permalink

The geek suggestion: Pass two PSI objects to a fragment of user-editable groovy code and let it generate the java code.

0
Comment actions Permalink

On 2007-02-09 12:11:11 +0300, ngokai <no_reply@jetbrains.com> said:

In intellij, while we use alt + insert and choose "equals() and hashcode()"
idea will generate the equals and hashcode method for us.
But I can't change how the equals/hashcode method's generation.
for example :
original code idea generate
if (o == null || getClass() != o.getClass()) return false;

code I want
if (o == null || !(o instanceif $class$)) return false;

The second one is more suitable while we are using something enhanced
by cglib and so on.


Next EAP build will have an option to generate instanceof check instead
of .getClass() equality.

0
Comment actions Permalink

Here a JIRA issue for warnings in generated hashCode() mehtod:
http://www.jetbrains.net/jira/browse/IDEA-14780

Here's one for warnings in generated equals() method:
http://www.jetbrains.net/jira/browse/IDEA-8944

The last one was closed as "Won't Fix", but I think you should at least reconsider:
a warning in an often-generated method is quite annoying.

Taras


0

Please sign in to leave a comment.