##### Overview

- Download PMD 4.2.5
- What's new in PMD 4.2.5
- 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
- Mvn 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
- Rule guidelines

##### For example

- Run PMD on a Sourceforge project

##### Rule Sets

- Index
- Android
- 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
- Project Info
- Project Reports
- Development Process

##### Legend

- External Link
- Opens in a new window

## Migration Rules

Contains rules about migrating from one JDK version to another. Don't use these rules directly, rather, use a wrapper ruleset such as migrating_to_13.xml.

### ReplaceVectorWithList

Since: PMD 3.4

Consider replacing Vector usages with the newer java.util.ArrayList if expensive threadsafe operation is not required.

This rule is defined by the following XPath expression:

    //Type/ReferenceType/ClassOrInterfaceType[@Image='Vector']

Example:

    public class Foo {
     void bar() {
        Vector v = new Vector();
     }
    }

### ReplaceHashtableWithMap

Since: PMD 3.4

Consider replacing this Hashtable with the newer java.util.Map

This rule is defined by the following XPath expression:

    //Type/ReferenceType/ClassOrInterfaceType[@Image='Hashtable']

Example:

        public class Foo {
         void bar() {
            Hashtable h = new Hashtable();
         }
        }

### ReplaceEnumerationWithIterator

Since: PMD 3.4

Consider replacing this Enumeration with the newer java.util.Iterator

This rule is defined by the following XPath expression:

    //ImplementsList/ClassOrInterfaceType[@Image='Enumeration']

Example:

    public class Foo implements Enumeration {
        private int x = 42;
        public boolean hasMoreElements() {
            return true;
        }
        public Object nextElement() {
            return String.valueOf(i++);
        }
    }

### AvoidEnumAsIdentifier

Since: PMD 3.4

Finds all places where 'enum' is used as an identifier.

This rule is defined by the following XPath expression:

    //VariableDeclaratorId[@Image='enum']

Example:

        public class A {
            public  class foo {
                String enum = "foo";
            }
        }

### AvoidAssertAsIdentifier

Since: PMD 3.4

Finds all places where 'assert' is used as an identifier.

This rule is defined by the following XPath expression:

    //VariableDeclaratorId[@Image='assert']

Example:

        public class A {
            public  class foo {
                String assert = "foo";
            }
        }

### IntegerInstantiation

Since: PMD 3.5

In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly.

This rule is defined by the following XPath expression:

    //PrimaryPrefix
     /AllocationExpression
      [not (ArrayDimsAndInits)
       and (ClassOrInterfaceType/@Image='Integer'
        or ClassOrInterfaceType/@Image='java.lang.Integer')]

Example:

    public class Foo {
     private Integer i = new Integer(0); // change to Integer i = Integer.valueOf(0);
    }

### ByteInstantiation

Since: PMD 4.0

In JDK 1.5, calling new Byte() causes memory allocation. Byte.valueOf() is more memory friendly.

This rule is defined by the following XPath expression:

    //PrimaryPrefix/AllocationExpression
    [not (ArrayDimsAndInits)
    and (ClassOrInterfaceType/@Image='Byte'
    or ClassOrInterfaceType/@Image='java.lang.Byte')]

Example:

    public class Foo {
    private Byte i = new Byte(0); // change to Byte i =
    Byte.valueOf(0);
    }

### ShortInstantiation

Since: PMD 4.0

In JDK 1.5, calling new Short() causes memory allocation. Short.valueOf() is more memory friendly.

This rule is defined by the following XPath expression:

    //PrimaryPrefix
    /AllocationExpression
    [not (ArrayDimsAndInits)
    and (ClassOrInterfaceType/@Image='Short'
    or ClassOrInterfaceType/@Image='java.lang.Short')]

Example:

    public class Foo {
    private Short i = new Short(0); // change to Short i =
    Short.valueOf(0);
    }

### LongInstantiation

Since: PMD 4.0

In JDK 1.5, calling new Long() causes memory allocation. Long.valueOf() is more memory friendly.

This rule is defined by the following XPath expression:

    //PrimaryPrefix
    /AllocationExpression
    [not (ArrayDimsAndInits)
    and (ClassOrInterfaceType/@Image='Long'
    or ClassOrInterfaceType/@Image='java.lang.Long')]

Example:

    public class Foo {
    private Long i = new Long(0); // change to Long i =
    Long.valueOf(0);
    }

### JUnit4TestShouldUseBeforeAnnotation

Since: PMD 4.0

In JUnit 3, the setUp method was used to set up all data entities required in running tests. JUnit 4 skips the setUp method and executes all methods annotated with @Before before all tests

This rule is defined by the following XPath expression:

    //ClassOrInterfaceBodyDeclaration[MethodDeclaration/MethodDeclarator[@Image='setUp']]
    [count(Annotation/MarkerAnnotation/Name[@Image='Before'])=0]

Example:

    public class MyTest {
        public void setUp() {
            bad();
        }
    }
    public class MyTest2 {
        @Before public void setUp() {
            good();
        }
    }

### JUnit4TestShouldUseAfterAnnotation

Since: PMD 4.0

In JUnit 3, the tearDown method was used to clean up all data entities required in running tests. JUnit 4 skips the tearDown method and executes all methods annotated with @After after running each test

This rule is defined by the following XPath expression:

    //ClassOrInterfaceBodyDeclaration[MethodDeclaration/MethodDeclarator[@Image='tearDown']]
    [count(Annotation/MarkerAnnotation/Name[@Image='After'])=0]

Example:

    public class MyTest {
        public void tearDown() {
            bad();
        }
    }
    public class MyTest2 {
        @After public void tearDown() {
            good();
        }
    }

### JUnit4TestShouldUseTestAnnotation

Since: PMD 4.0

In JUnit 3, the framework executed all methods which started with the word test as a unit test. In JUnit 4, only methods annotated with the @Test annotation are executed.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceBodyDeclaration[MethodDeclaration/MethodDeclarator[starts-with(@Image,'test')]]
    [count(Annotation//Name[@Image='Test'])=0]

Example:

    public class MyTest {
        public void testBad() {
            doSomething();
        }

    	@Test
        public void testGood() {
            doSomething();
        }
    }

### JUnit4SuitesShouldUseSuiteAnnotation

Since: PMD 4.0

In JUnit 3, test suites are indicated by the suite() method. In JUnit 4, suites are indicated through the @RunWith(Suite.class) annotation.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceBodyDeclaration[MethodDeclaration/MethodDeclarator[@Image='suite']]
    [MethodDeclaration/ResultType/Type/ReferenceType/ClassOrInterfaceType[@Image='Test' or @Image = 'junit.framework.Test']]
    [not(MethodDeclaration/Block//ClassOrInterfaceType[@Image='JUnit4TestAdapter'])]

Example:

    public class BadExample extends TestCase{

        public static Test suite(){
        	return new Suite();
        }
    }

    @RunWith(Suite.class)
    @SuiteClasses( { TestOne.class, TestTwo.class })
    public class GoodTest {
    }

### JUnitUseExpected

Since: PMD 4.0

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

Example:

    public class MyTest {
    	@Test
        public void testBad() {
            try {
                doSomething();
                fail("should have thrown an exception");
            } catch (Exception e) {
            }
        }

    	@Test(expected=Exception.class)
        public void testGood() {
            doSomething();
        }
    }