Files
2025-02-05 04:43:28 +00:00

44 lines
1.9 KiB
HTML

<html>
<body>
Reports classes implementing the <code>Cloneable</code> interface that don't override the
<code>clone()</code> method.
<p>Such classes use the default implementation of <code>clone()</code>,
which isn't <code>public</code> but <code>protected</code>, and which does not copy the mutable state of the class.</p>
<p>A quick-fix is available to generate a basic <code>clone()</code> method,
which can be used as a basis for a properly functioning <code>clone()</code> method
expected from a <code>Cloneable</code> class.</p>
<p><b>Example:</b></p>
<pre><code>
<b>public class</b> Data <b>implements</b> Cloneable {
<b>private</b> String[] names;
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
<b>public class</b> Data <b>implements</b> Cloneable {
<b>private</b> String[] names;
@Override
<b>public</b> Data clone() {
<b>try</b> {
Data clone = (Data) <b>super</b>.clone();
// TODO: copy mutable state here, so the clone can't change the internals of the original
<b>return</b> clone;
} <b>catch</b> (CloneNotSupportedException e) {
<b>throw new</b> AssertionError();
}
}
}
</code></pre>
<!--
Note for translators: the text of the TODO comment in the example code above comes from
community/plugins/InspectionGadgets/InspectionGadgetsAnalysis/resources/messages/InspectionGadgetsBundle.properties
property key: cloneable.class.without.clone.todo.message
-->
<!-- tooltip end -->
<p>Use the <b>Ignore classes cloneable due to inheritance</b> option to ignore classes that are
<code>Cloneable</code> because they inherit from the <code>Cloneable</code> class.</p>
<p>Use the <b>Ignore when Cloneable is necessary to call clone() method of super class</b>
option to ignore classes that require implementing <code>Cloneable</code> because they call the <code>clone()</code> method from a superclass.</p>
</body>
</html>