rspec/rules/S2097/java/rule.adoc

46 lines
874 B
Plaintext

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
[source,java]
----
public boolean equals(Object obj) { // Noncompliant
MyClass mc = (MyClass)obj;
// ...
}
----
== Compliant Solution
[source,java]
----
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
MyClass mc = (MyClass)obj;
// ...
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]