rspec/rules/S2161/java/rule.adoc

45 lines
889 B
Plaintext
Raw Normal View History

Non ``++final++`` classes shouldn't use a hardcoded class name in the ``++equals++`` method. Doing so breaks the method for subclasses. Instead, make the comparison dynamic.
== Noncompliant Code Example
----
public class Fruit {
private Season ripe;
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (Fruit.class == obj.class) { // Noncompliant
return false;
}
// ...
----
== Compliant Solution
----
public class Fruit {
private Season ripe;
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (this.class == obj.class) { // will work for subclasses too
return false;
}
// ...
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]