A Student's Guide to Software Engineering Tools & Techniques »

PMD

Author: Xiao Pu

Overview

PMD is a static analyser for Java, JavaScript, Salesforce.com Apex, PL/SQL, Apache Velocity, XML, XSL. The copy/paste-detector(CPD), which helps to find duplicated code, is also included as an add-on in PMD.

Note that the links in this chapter is based on PMD version 5.5.3. You can check the latest version and documentation at its GitHub Page.

Features

Rules in PMD represent patterns in code. PMD is supposed to check these rules(patterns) and signal violations to programmers. For example, the OverrideBothEqualsAndHashcode rule requires programmers to override both public boolean Object.equals(Object other), and public int Object.hashCode(), or override neither.

PMD supports checking rules for the following languages.

PMD doesn't support checking rules for the following languages. Only Copy/Paste Detector (CPD) is supported for them.

Limitation

Limitations are almost the same as CheckStyle.

  • The examined code has to be compilable. The reason is described in How does it work section.
  • Files will be examined one by one, which means you cannot check multiple files at the same time.
    • For example, you cannot determine the full inheritance hierarchy of a class as you need to examine the parent class while checking the child class.

How to Use It?

Download

PMD can be run on both Windows and Linux/Unix operating system with the help of Java JRE 1.7 or higher. Refer to How to install PMD and CPD for more details.

Configuration

You can configure PMD to only include the rules that your want (see How to make a new rule set).

Suppress Warnings

PMD supports suppressing warnings in four ways:

  • Annotations
  • Comments
  • Violation Suppress Regex
  • Violation Suppress XPath

The details are described here.

Command Line Usage

PMD can be launched by using command line with various arguments. For details, please refer to Running PMD via command line.

Integration With Build Automation Tools

Integration With IDEs

PMD can be integrated with most of IDEs, inlcuding BlueJ, CodeGuide, Eclipse, eclipse-pmd, Emacs, Gel, IntelliJ IDEA, IntelliJ IDEA - QAPlug, JBuilder, JCreator, JDeveloper, JEdit, Maven, Maven 2, NetBeans, TextPad, WebLOgic Workshop 8.1.x. For instruction to integrate with those IDEs, please refer to PMD integrations.

Available Rulesets

There is not pre-defined rules set. You need to define your rule sets by yourself.

PMD has organised rules into different categories. For example, the rules for Java has been categorised into 26 sections, which will help you quickly find the rules that you want.

You may refer to the Features section to view the rules according to your languages and refer to Configuration section to configure your rulesets.

How Does it Work?

PMD use JavaCC to parse your code to a AST(Abstract Syntax Tree) and visited it recursively (more details). Thus, one of the requirements for PMD to work is that the code must be in valid Java syntax. You can view the syntax tree by using bin/designer.bat

Customization

Resources

  • PMD: PMD official website. You can download the latest version, view online documentation there.
  • PMD Github: PMD GitHub page. You can contribute to the project or report bugs there.
  • StackOverflow PMD: Question/Answer forum in StackOverflow for PMD. You can ask question related to the using of PMD.