rspec/rules/S2097/java/rule.adoc

26 lines
593 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Because the ``++equals++`` method takes a generic ``++Object++`` as a parameter, any type of object may be passed to it. The method should not assume it will only be used to test objects of its class type. It must instead check the parameter's type.
== Noncompliant Code Example
----
public boolean equals(Object obj) {
MyClass mc = (MyClass)obj; // Noncompliant
// ...
}
----
== Compliant Solution
----
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
MyClass mc = (MyClass)obj;
// ...
}
----