80 lines
3.0 KiB
Plaintext
80 lines
3.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Due to the similar name with the methods `Object.toString`, `Object.hashCode` and `Object.equals`,
|
|
there is a significant likelihood that a developer intended to override one of these methods but made a spelling error.
|
|
|
|
Even if no such error exists and the naming was done on purpose, these method names can be misleading.
|
|
Readers might not notice the difference, or if they do, they may falsely assume that the developer made a mistake.
|
|
|
|
== How to fix it
|
|
|
|
If you intended to override one of the methods `Object.toString`, `Object.hashCode`, or `Object.equals`, correct the spelling.
|
|
Also, you should add the `@Override` modifier, which causes a compiler error message
|
|
in case the annotated method does not override anything.
|
|
|
|
If the naming was done on purpose, you should rename the methods to be more distinctive.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public int hashcode() { /* ... */ } // Noncompliant
|
|
|
|
public String tostring() { /* ... */ } // Noncompliant
|
|
|
|
public boolean equal(Object obj) { /* ... */ } // Noncompliant
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
@Override
|
|
public int hashCode() { /* ... */ } // Compliant
|
|
|
|
@Override
|
|
public String toString() { /* ... */ } // Compliant
|
|
|
|
@Override
|
|
public boolean equals(Object obj) { /* ... */ } // Compliant
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Either override "[Object.hashCode()|Object.equals]", or totally rename the method to prevent any confusion.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== relates to: S1201
|
|
|
|
=== is related to: S1845
|
|
|
|
=== on 11 Feb 2015, 18:15:50 Nicolas Peru wrote:
|
|
@Ann I linked this issue to RSPEC-1201, I'll let you update description and findbugs reference.
|
|
|
|
=== on 11 Feb 2015, 20:25:04 Ann Campbell wrote:
|
|
\[~nicolas.peru] are you saying you'd like to roll the content of this rule into RSPEC-1201? Because they're actually about (slightly) different things...
|
|
|
|
=== on 12 Feb 2015, 07:18:50 Nicolas Peru wrote:
|
|
\[~ann.campbell.2] I am saying that this rule should not cover the equals method as it is covered by RSPEC-1201. I don't see the difference between the two rules except that one concern equals and the other one hashcode.
|
|
|
|
=== on 12 Feb 2015, 13:53:05 Ann Campbell wrote:
|
|
\[~nicolas.peru], this rule doesn't cover "equals" (with an 's'), it covers "equal". I.e. this rule is about method names that are not-quite-right. RSPEC-1201 is about signatures that are not-quite-right. Or are you saying that in the implementation of RSPEC-1201 you also check this spelling variation?
|
|
|
|
=== on 12 Feb 2015, 14:23:05 Nicolas Peru wrote:
|
|
\[~ann.campbell.2]My bad, I really did not spot the difference between the two rules... I tend to think this one should clearly mention that it detects typo. So this mean that not only description but implementation is out of date in plugin. ticket created : \http://jira.codehaus.org/browse/SONARJAVA-902
|
|
|
|
endif::env-github,rspecator-view[]
|