PMD

PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It supports Java, JavaScript, PLSQL, Apache Velocity, XML, XSL.

Installation

Download "pmd-bin-5.3.3.zip" from Sourceforge

unzip pmd-bin-5.3.3.zip
cd pmd-bin-5.3.3/bin

Run "pmd.bat" if you are using Windows or "run.sh" if you are using Linux.

Usage

Example on Windows:
C:\>pmd-bin-5.3.2\bin\pmd.bat -dir c:\my\source\code -format text -R java-unusedcode,java-imports -version 1.5 -language java -debug
C:\>pmd-bin-5.3.2\bin\pmd.bat -dir c:\my\source\code -f xml -rulesets java-basic,java-design -encoding UTF-8
C:\>pmd-bin-5.3.2\bin\pmd.bat -d c:\my\source\code -rulesets java-typeresolution -auxclasspath commons-collections.jar;derby.jar
C:\>pmd-bin-5.3.2\bin\pmd.bat -d c:\my\source\code -f html -R java-typeresolution -auxclasspath file:///C:/my/classpathfile
Example on Linux:
$ pmd-bin-5.3.2/bin/run.sh pmd -dir /home/workspace/src/main/java/code -f html -rulesets java-basic,java-design,java-sunsecure
$ pmd-bin-5.3.2/bin/run.sh pmd -d ./src/main/java/code -f xslt -R java-basic,java-design -property xsltFilename=my-own.xsl
$ pmd-bin-5.3.2/bin/run.sh pmd -d ./src/main/java/code -f html -R java-typeresolution -auxclasspath commons-collections.jar:derby.jar

PMD Rulesets for Java

Ruleset Rule name Rule description
java-android
Android These rules deal with the Android SDK, mostly related to best practices.To get better results, make sure that the auxclasspath is defined for type resolution to work.
java-basic
Basic The Basic ruleset contains a collection of good practices which should be followed.
java-braces
Braces The Braces ruleset contains rules regarding the use and placement of braces.
java-clone
Clone Implementation The Clone Implementation ruleset contains a collection of rules that find questionable usages of the clone() method.
java-codesize
Code Size The Code Size ruleset contains rules that find problems related to code size or complexity.
java-comments
Comments Rules intended to catch errors related to code comments
java-controversial
Controversial The Controversial ruleset contains rules that, for whatever reason, are considered controversial.They are held here to allow people to include them as they see fit within their custom rulesets.
java-coupling
Coupling Rules which find instances of high or inappropriate coupling between objects and packages.
java-design
Design The Design ruleset contains rules that flag suboptimal code implementations. Alternate approachesare suggested.
java-empty
Empty Code The Empty Code ruleset contains rules that find empty statements of any kind (empty method,empty block statement, empty try or catch block,…).
java-finalizers
Finalizer These rules deal with different problems that can occur with finalizers.
java-imports
Import Statements These rules deal with different problems that can occur with import statements.
java-j2ee
J2EE Rules specific to the use of J2EE implementations.
java-javabeans
JavaBeans The JavaBeans Ruleset catches instances of bean rules not being followed.
java-junit
JUnit These rules deal with different problems that can occur with JUnit tests.
java-logging
-jakarta-commons
Jakarta Commons Logging The Jakarta Commons Logging ruleset contains a collection of rules that find questionable usages of that framework.
java-logging-java
Java Logging The Java Logging ruleset contains a collection of rules that find questionable usages of the logger.
java-migrating
Migration 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.
java-migrating_to_13
Migration (see above)
java-migrating_to_14
Migration (see above)
java-migrating_to_15
Migration (see above)
java-migrating_to_junit4
Migration (see above)
java-naming
Naming The Naming Ruleset contains rules regarding preferred usage of names and identifiers.
java-optimizations
Optimization These rules deal with different optimizations that generally apply to best practices.
java-strictexception
Strict Exceptions These rules provide some strict guidelines about throwing and catching exceptions.
java-strings
String and StringBuffer These rules deal with different issues that can arise with manipulation of the String, StringBuffer, or StringBuilder instances.
java-sunsecure
Security Code Guidelines These rules check the security guidelines from Sun, published at this page
java-typeresolution
Type Resolution These are rules which resolve java Class files for comparison, as opposed to a String
java-unnecessary
Unnecessary The Unnecessary Ruleset contains a collection of rules for unnecessary code.
java-unusedcode
Unused Code The Unused Code ruleset contains rules that find unused or ineffective code.

PMD Rulesets for JavaScript

Ruleset Rule name Rule description
ecmascript-basic
Basic Ecmascript Rules concerning basic ECMAScript guidelines.
ecmascript-braces
Braces The Braces Ruleset contains a collection of braces rules.
ecmascript-controversial
Controversial Ecmascript The Controversial ruleset contains rules that, for whatever reason, are considered controversial.They are held here to allow people to include them as they see fit within their custom rulesets.
ecmascript-unnecessary
Unnecessary The Unnecessary Ruleset contains a collection of rules for unnecessary code.

Further Reading