rspec/rules/S4351/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
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.
2023-05-25 14:18:12 +02:00

65 lines
1.3 KiB
Plaintext

== Why is this an issue?
When implementing the ``++Comparable<T>.compareTo++`` method, the parameter's type has to match the type used in the ``++Comparable++`` declaration. When a different type is used this creates an overload instead of an override, which is unlikely to be the intent.
This rule raises an issue when the parameter of the ``++compareTo++`` method of a class implementing ``++Comparable<T>++`` is not same as the one used in the ``++Comparable++`` declaration.
=== Noncompliant code example
[source,java]
----
public class Foo {
static class Bar implements Comparable<Bar> {
public int compareTo(Bar rhs) {
return -1;
}
}
static class FooBar extends Bar {
public int compareTo(FooBar rhs) { // Noncompliant: Parameter should be of type Bar
return 0;
}
}
}
----
=== Compliant solution
[source,java]
----
public class Foo {
static class Bar implements Comparable<Bar> {
public int compareTo(Bar rhs) {
return -1;
}
}
static class FooBar extends Bar {
public int compareTo(Bar rhs) {
return 0;
}
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor this method so that its argument is of type 'xxx'
=== Highlighting
compareTo declaration
endif::env-github,rspecator-view[]