SuperClone

Since Checkstyle 3.2

Description

Checks that an overriding clone() method invokes super.clone(). Does not check native methods, as they have no possible java defined implementation.

Reference: Object.clone().

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="SuperClone"/>
  </module>
</module>
        

Example:

class Example1 {
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}

class SuperCloneB {
  private int b;

  // violation below, "Method 'clone' should call 'super.clone'."
  public SuperCloneB clone() {
    SuperCloneB other = new SuperCloneB();
    other.b = this.b;
    return other;
  }
}

class SuperCloneC {

  public SuperCloneC clone() throws CloneNotSupportedException {
    return (SuperCloneC) super.clone();
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker