##### Overview

- Download PMD 3.7
- What's new in PMD 3.7
- PMD in the news
- PMD-related products and books
- Best practices
- Future directions
- Similar projects
- Credits
- License
- What does 'PMD' mean?

##### Usage

- Installation
- Command line usage
- Ant task usage
- Maven plugin usage
- IDE plugin usage
- Suppressing warnings
- Finding duplicated code
- JSP support

##### Customizing PMD

- Compiling PMD
- How to write a rule
- Writing XPath rules
- How to make a rule set
- How it works

##### For example

- Run PMD on a Sourceforge project

##### Rule Sets

- Index
- Basic
- Braces
- Code Size
- Clone
- Controversial
- Coupling
- Design
- Finalizers
- Import Statements
- J2EE
- Javabeans
- JUnit Tests
- Logging (Java)
- Logging (Jakarta)
- Migrating
- Naming
- Optimizations
- Strict Exceptions
- Strings
- Sun Security
- Unused Code
- Java Server Pages
- Java Server Faces

##### Project Documentation

- About PMD
- Project Info
- Project Reports
- Development Process

## Clone Implementation Rules

The Clone Implementation ruleset contains a collection of rules that find questionable usages of the clone() method.

### ProperCloneImplementation

Object clone() should be implemented with super.clone()

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration//MethodDeclarator
    [@Image = 'clone']
    [count(FormalParameters/*) = 0]
    [count(../Block//*[
        (self::AllocationExpression) and
        (./ClassOrInterfaceType/@Image = ancestor::
    ClassOrInterfaceDeclaration[position()=last()]/@Image)
      ])&gt; 0
    ]

Here's an example of code that would trigger this rule:

    class Foo{
        public Object clone(){
            return new Foo(); // This is bad
        }
    }

### CloneThrowsCloneNotSupportedException

The method clone() should throw a CloneNotSupportedException

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
    [@Final = 'false']
    [.//MethodDeclaration[
    MethodDeclarator/@Image = 'clone'
    and count(MethodDeclarator/FormalParameters/*) = 0
    and count(NameList/Name[contains
    (@Image,'CloneNotSupportedException')]) = 0]]

Here's an example of code that would trigger this rule:

     public class MyClass implements Cloneable{
         public Object clone() // will cause an error {
              MyClass clone = (MyClass)super.clone();
              ...
              return clone;
         }
     }

### CloneMethodMustImplementCloneable

The method clone() should only be implemented if the class implements the Cloneable interface

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
    [not(./ImplementsList/ClassOrInterfaceType
    [@Image='Cloneable'])]
    [.//MethodDeclaration/MethodDeclarator[@Image
    = 'clone' and count(FormalParameters/*) = 0]]

Here's an example of code that would trigger this rule:

    public class MyClass {
     public Object clone() throws CloneNotSupportedException {
      return foo;
     }
    }