
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
59 lines
1.3 KiB
Plaintext
59 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
By contract, the ``++equals(Object)++`` method, from ``++java.lang.Object++``, should accept a ``++null++`` argument. Among all the other cases, the ``++null++`` case is even explicitly detailed in the ``++Object.equals(...)++`` Javadoc, stating _"For any non-null reference value x, x.equals(null) should return false."_
|
|
|
|
|
|
Assuming that the argument to ``++equals++`` is always non-null, and enforcing that assumption with an annotation is not only a fundamental violation of the contract of ``++equals++``, but it is also likely to cause problems in the future as the use of the class evolves over time.
|
|
|
|
|
|
The rule raises an issue when the ``++equals++`` method is overridden and its parameter annotated with any kind of ``++@Nonnull++`` annotation.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public boolean equals(@javax.annotation.Nonnull Object obj) { // Noncompliant
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public boolean equals(Object obj) {
|
|
if (obj == null) {
|
|
return false;
|
|
}
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this non-null annotation.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
annotation
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== relates to: S2638
|
|
|
|
endif::env-github,rspecator-view[]
|