rspec/rules/S2158/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

53 lines
1.4 KiB
Plaintext

== Why is this an issue?
``++Cloneable++`` is the marker ``++Interface++`` that indicates that ``++clone()++`` may be called on an object. Overriding ``++clone()++`` without implementing ``++Cloneable++`` can be useful if you want to control how subclasses clone themselves, but otherwise, it's probably a mistake.
=== Noncompliant code example
[source,java]
----
class Team { // Noncompliant
private Person coach;
private List<Person> players;
public void addPlayer(Person p) { ... }
public Person getCoach() { ... }
@Override
public Team clone() { ... }
}
----
=== Compliant solution
[source,java]
----
class Team implements Cloneable {
private Person coach;
private List<Person> players;
public void addPlayer(Person p) { ... }
public Person getCoach() { ... }
@Override
public Team clone() { ... }
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
=== on 25 Nov 2014, 16:35:47 Freddy Mallet wrote:
My 2 cents @Ann: I would merge this rule with RSPEC-1182 and so will also change the title of RSPEC-1182 like this :
"When overriding Object.clone(), Cloneable should be implemented and super.clone() should be called"
Indeed, that's an issue when overriding the Object.clone() method to not implement the Cloneable interface if and only if super.clone() method is called.
endif::env-github,rspecator-view[]