rspec/rules/S4351/java/rule.adoc

55 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
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;
}
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
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)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]