##### Overview

- Download PMD 3.9
- What's new in PMD 3.9
- 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

## Controversial Rules

The Controversial Ruleset contains rules that, for whatever reason, are considered controversial. They are separated out here to allow people to include as they see fit via custom rulesets. This ruleset was initially created in response to discussions over UnnecessaryConstructorRule which Tom likes but most people really dislike :-)

### UnnecessaryConstructor

This rule detects when a constructor is not necessary; i.e., when there's only one constructor, it's public, has an empty body, and takes no arguments.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceBody[count(ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)=1]
    /ClassOrInterfaceBodyDeclaration/ConstructorDeclaration
    [@Public='true']
    [not(FormalParameters/*)]
    [not(BlockStatement)]
    [not(NameList)]
    [count(ExplicitConstructorInvocation/Arguments/ArgumentList/Expression)=0]

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

    public class Foo {
     public Foo() {}
    }

### NullAssignment

Assigning a "null" to a variable (outside of its declaration) is usually bad form. Some times, the assignment is an indication that the programmer doesn't completely understand what is going on in the code. NOTE: This sort of assignment may in rare cases be useful to encourage garbage collection. If that's what you're using it for, by all means, disregard this rule :-)

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.NullAssignmentRule

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

     public class Foo {
       public void bar() {
         Object x = null; // This is OK.
         x = new Object();
         // Big, complex piece of code here.
         x = null; // This is BAD.
         // Big, complex piece of code here.
       }
     }

### OnlyOneReturn

A method should have only one exit point, and that should be the last statement in the method.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.OnlyOneReturnRule

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

     public class OneReturnOnly1 {
      public void foo(int x) {
       if (x &gt; 0) {
        return "hey";   // oops, multiple exit points!
       }
       return "hi";
      }
     }

### UnusedModifier

Fields in interfaces are automatically public static final, and methods are public abstract. Classes or interfaces nested in an interface are automatically public and static (all nested interfaces are automatically static). For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.UnusedModifier

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

    public interface Foo {
     public abstract void bar(); // both abstract and public are ignored by the compiler
     public static final int X = 0; // public, static, and final all ignored
     public static class Bar {} // public, static ignored
     public static interface Baz {} // ditto
    }
    public class Bar {
     public static interface Baz {} // static ignored
    }

### AssignmentInOperand

Avoid assignments in operands; this can make code more complicated and harder to read.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.AssignmentInOperand

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

    public class Foo {
     public void bar() {
      int x = 2;
      if ((x = getX()) == 3) {
       System.out.println("3!");
      }
     }
     private int getX() {
      return 3;
     }
    }

### AtLeastOneConstructor

Each class should declare at least one constructor.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
     [not(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)]
     [@Interface='false']

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

    public class Foo {
     // no constructor!  not good!
    }

### DontImportSun

Avoid importing anything from the 'sun.*' packages. These packages are not portable and are likely to change.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.imports.DontImportSun

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

    import sun.misc.foo;
    public class Foo {}

### SuspiciousOctalEscape

A suspicious octal escape sequence was found inside a String literal. The Java language specification (section 3.10.6) says an octal escape sequence inside a literal String shall consist of a backslash followed by: OctalDigit | OctalDigit OctalDigit | ZeroToThree OctalDigit OctalDigit Any octal escape sequence followed by non-octal digits can be confusing, e.g. "\038" is interpreted as the octal escape sequence "\03" followed by the literal character "8".

This rule is defined by the following Java class: net.sourceforge.pmd.rules.SuspiciousOctalEscape

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

    public class Foo {
     public void foo() {
      // interpreted as octal 12, followed by character '8'
      System.out.println("suspicious: \128");
     }
    }

### CallSuperInConstructor

It is a good practice to call super() in a constructor. If super() is not called but another constructor (such as an overloaded constructor) is called, this rule will not report it.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration[ count (ExtendsList/*) &gt; 0 ]
    /ClassOrInterfaceBody
     /ClassOrInterfaceBodyDeclaration
     /ConstructorDeclaration[ count (.//ExplicitConstructorInvocation)=0 ]

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

    public class Foo extends Bar{
     public Foo() {
      // call the constructor of Bar
      super();
     }
     public Foo(int code) {
      // do something with code
      this();
      // no problem with this
     }
    }

### UnnecessaryParentheses

Sometimes expressions are wrapped in unnecessary parentheses, making them look like a function call.

This rule is defined by the following XPath expression:

    //ReturnStatement
              /Expression
               /PrimaryExpression
                /PrimaryPrefix
                 /Expression[count(*)=1]
                  /PrimaryExpression
                  /PrimaryPrefix

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

      public class Foo {
          boolean bar() {
              return (true);
          }
      }

### SingularField

A field that's only used by one method could perhaps be replaced by a local variable.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.SingularField

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

    public class Foo {
        private int x;
        public void foo(int y) {
         x = y + 5;
         return x;
        }
    }

### DefaultPackage

Use explicit scoping instead of the default package private level.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration[@Interface='false']
    /ClassOrInterfaceBody
    /ClassOrInterfaceBodyDeclaration
    [
    FieldDeclaration[@PackagePrivate='true']
    or MethodDeclaration[@PackagePrivate='true']
    ]

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

### BooleanInversion

Use bitwise inversion to invert boolean values - it's the fastest way to do this. See http://www.javaspecialists.co.za/archive/newsletter.do?issue=042&locale=en_US for specific details

This rule is defined by the following XPath expression:

    //AssignmentOperator[@Image="="]/../Expression/UnaryExpressionNotPlusMinus[@Image="!"]

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

    public class Foo {
     public void main(bar) {
      boolean b = true;
      b = !b; // slow
      b ^= true; // fast
     }
    }

### DataflowAnomalyAnalysis

The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow. From those informations there can be found various problems. 1. UR - Anomaly: There is a reference to a variable that was not defined before. This is a bug and leads to an error. 2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text. 3. DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.

This rule is defined by the following Java class: net.sourceforge.pmd.dfa.DaaRule

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

    public class Foo {
        public void foo() {
    	 int buz = 5;
    	 buz = 6; // redefinition of buz -&gt; dd-anomaly
    	 foo(buz);
    	 buz = 2;
        } // buz is undefined when leaving scope -&gt; du-anomaly
    }

This rule has the following properties:

Name | Default value | Description
maxviolations | 100 |
maxpaths | 1000 |