Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.puppycrawl.tools.checkstyle.checks.coding
Class NoCloneCheck

    java.lang.Object
      com.puppycrawl.tools.checkstyle.api.AutomaticBean
          com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
              com.puppycrawl.tools.checkstyle.api.Check
                  com.puppycrawl.tools.checkstyle.checks.coding.AbstractIllegalMethodCheck
                      com.puppycrawl.tools.checkstyle.checks.coding.NoCloneCheck

All Implemented Interfaces:
  Configurable, Contextualizable

    public class NoCloneCheck

extends AbstractIllegalMethodCheck

Checks that the clone method is not overridden from the Object class.

Rationale: The clone method relies on strange/hard to follow rules that do not work it all situations. Consequently, it is difficult to override correctly. Below are some of the rules/reasons why the clone method should be avoided.

- Classes supporting the clone method should implement the Cloneable interface but the Cloneable interface does not include the clone method. As a result, it doesn't enforce the method override.
- The Cloneable interface forces the Object's clone method to work correctly. Without implementing it, the Object's clone method will throw a CloneNotSupportedException.
- Non-final classes must return the object returned from a call to super.clone().
- Final classes can use a constructor to create a clone which is different from non-final classes.
- If a super class implements the clone method incorrectly all subclasses calling super.clone() are doomed to failure.
- If a class has references to mutable objects then those object references must be replaced with copies in the clone method after calling super.clone().
- The clone method does not work correctly with final mutable object references because final references cannot be reassigned.
- If a super class overrides the clone method then all subclasses must provide a correct clone implementation.

Two alternatives to the clone method, in some cases, is a copy constructor or a static factory method to return copies of an object. Both of these approaches are simpler and do not conflict with final fields. The do not force the calling client to handle a CloneNotSuportException. They also are typed therefore no casting is necessary. Finally, they are more flexible since they can take interface types rather than concrete classes.

Sometimes a copy constructor or static factory is not an acceptable alternative to the clone method. The example below highlights the limitation of a copy constructor (or static factory). Assume Square is a subclass for Shape.

     Shape s1 = new Square();
     System.out.println(s1 instanceof Square); //true

...assume at this point the code knows nothing of s1 being a Square that's the beauty of polymorphism but the code wants to copy the Square which is declared as a Shape, its super type...

     Shape s2 = new Shape(s1); //using the copy constructor
     System.out.println(s2 instanceof Square); //false

The working solution (without knowing about all subclasses and doing many casts) is to do the following (assuming correct clone implementation).

     Shape s2 = s1.clone();
     System.out.println(s2 instanceof Square); //true

Just keep in mind if this type of polymorphic cloning is required then a properly implemented clone method may be the best choice.

Much of this information was taken from Effective Java: Programming Language Guide First Edition by Joshua Bloch pages 45-52. Give Bloch credit for writing an excellent book.

This check is almost exactly the same as the `NoFinalizerCheck`

Version:
  1.0
Author:
  Travis Schneeberger
See Also:
  `Object.clone()`, `Cloneable`, `CloneNotSupportedException`

Constructor Summary
`NoCloneCheck()` Creates an instance.

Method Summary

Methods inherited from class com.puppycrawl.tools.checkstyle.checks.coding.AbstractIllegalMethodCheck
`getDefaultTokens, visitToken`

Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check
`beginTree, destroy, finishTree, getAcceptableTokens, getClassLoader, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens`

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
`getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity`

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
`configure, contextualize, finishLocalSetup, getConfiguration, setupChild`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### NoCloneCheck

    public NoCloneCheck()

  Creates an instance.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2001-2011. All Rights Reserved.