Inspectopedia Help

Cloneable class without 'clone()' method

Reports classes implementing the Cloneable interface that don't override the clone() method.

Such classes use the default implementation of clone(), which isn't public but protected, and which does not copy the mutable state of the class.

A quick-fix is available to generate a basic clone() method, which can be used as a basis for a properly functioning clone() method expected from a Cloneable class.

Example:

public class Data implements Cloneable { private String[] names; }

After the quick-fix is applied:

public class Data implements Cloneable { private String[] names; @Override public Data clone() { try { Data clone = (Data) super.clone(); // TODO: copy mutable state here, so the clone can't change the internals of the original return clone; } catch (CloneNotSupportedException e) { throw new AssertionError(); } } }

Use the Ignore classes cloneable due to inheritance option to ignore classes that are Cloneable because they inherit from the Cloneable class.

Use the Ignore when Cloneable is necessary to call clone() method of super class option to ignore classes that require implementing Cloneable because they call the clone() method from a superclass.

Inspection options

Option

Type

Default

Ignore classes cloneable due to inheritance

Checkbox

true

Ignore if Cloneable is necessary to call the clone() method of a superclass

Checkbox

true

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023