Clone Standard Signature
ID |
java.clone_standard_signature |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
clone, reliability |
Description
Reports clone() methods with non-standard return types. The clone() method must return Object (or a covariant subtype), never void.
Rationale
A void clone() method cannot be used by the Cloneable mechanism and confuses callers who expect a copy to be returned.
// Bad \u2014 void return
public void clone() throws CloneNotSupportedException {
// cannot return a copy
}
Remediation
Return Object (or the class type via covariant return):
// Good
public Object clone() throws CloneNotSupportedException {
return super.clone();
}