PMD (Programming Mistake Detector)

PMD (Programming Mistake Detector)

What is PMD?

  • PMD aka Programming Mistake Detector is Java Source Code Analyzer.
  • It is used to clean erroneous code in our java projects based on predefined set of rules.
  • PMD supports the ability to write custom rules.
  • Issues reported by PMD may not be true errors always, but rather inefficient code, i.e. the application could still function properly even if they were not corrected.

PMD works by scanning Java code and checks for violations in three major areas:

  • Compliance with coding standards such as:
    • Naming conventions – class, method, parameter and variable names
    • Class and method length
    • Existence and formatting of comments and JavaDocs
  • Coding anti-patterns such as:
    • Empty try/catch/finally/switch blocks
    • Unused local variables, parameters and private methods
    • Empty if/while statements
    • Over-complicated expressions – unnecessary if statements, for loops that could be while loops
    • Classes with high Cyclomatic Complexity measurements
  • Cut and Paste Detector (CPD)– a tool that scans files and looks for suspect code replication. CPD can be parameterized by the minimum size of the code block.

PMD is able to detect flaws or possible flaws in source code, like:

  • Possible bugs —Empty try/catch/finally/switch blocks.
  • Dead code —Unused local variables, parameters and private methods.
  • Empty if/while statements.
  • Over-complicated expressions—Unnecessary if statements, for loops that could be while loops.
  • Sub-optimal code—Wasteful String/StringBuffer usage.
  • Duplicate code—Copied/pasted code can mean copied/pasted bugs, and decreases maintainability.

How to install PMD?

The easiest way to install PMD is by using the remote update site. Users behind firewalls should check proxy settings before going any further. If these settings are mis-configured the updater will not work. PMD also supplies a zip file for manual installation. Download the file and follow the readme.

Demonstrated below is installing PMD via the Eclipse Software Updater.

  1. Launch Eclipse.
  2. Navigate to HelpSoftware UpdatesFind and Install…
  3. Select “Search for new features to install” and click Next
  4. Click New Remote Site…
  5. Enter a name (PMD) and the URL
  6. In Sites to include in searchcheck PMD and click Finish
  7. In the Search Resultsdialog check PMD for Eclipse 3 3.1.0 and click NextPMD2
  8. Accept the terms of the license agreements and click Next
  9. Click Finish.
  10. Wait for Eclipse to download the required jar files, then click Install
  11. Restart the workbench. This will load the PMD plugin.
  12. Navigate to WindowShow View | ..
  13. Select PMDPMD Violations
  14. PMD Violations view should appear at the bottom pane

How to use PMD?

Before launching Eclipse make sure you have enough memory for PMD. This is particularly important when analyzing large projects. In these situations PMD tends to be memory-hungry. Hence, make sure to start with as much memory as you can afford, for example 512M (eclipse.exe -vmargs -Xmx512M)

  1. Launch Eclipse
  2. If you have previously created a Java Project, skip to Step 6. Otherwise click FileNew..
  3. Select Java Projectand click Next
  4. Enter a project name (QA Project) and leave everything else in the default state.PMD3
  5. Click Finish. Eclipse will ask if you want to switch to the Java Perspective. Click Yes.
  6. In the Package Explorer right-click on QA Project and select NewClass
  7. In the following dialog enter the class name as Yingand click Finish
  8. A new class Yingis created in project’s default package. Paste the following code into the new class:
  9. In the Package Explorer right-click on QA Project and select PMDCheck Code With PMDPMD4
  10. Wait for PMD to scan Yingand Yang
  11. If PMD Violations view is not open navigate to WindowShow View | .. and select PMD | PMD Violations
  12. In the PMD Violationsview notice a list of 17 violations. In large projects this list could easily grow up to several thousand. This is one of the reasons PMD allows violations be filtered by priority. Priority is a configurable attribute of a PMD rule. PMD assigns priorities from 1 to 5 and each priority is represented by a colored square at the top-right corner of the view. These little squares are actually clickable on-off switches used to control the visibility of the violations they represent.
    The results table itself is well laid out and most columns are sortable. It is also possible to get more detail on a violation by simply right-clicking it and selecting Show rule. PMD pops-up a dialog with information such as rule name, implementation class, message, description and an example. This feature can be helpful when trying to makes sense of a new rule or letting inhouse developers know about a particular company rule or coding convention.


Finally, in the PMD Violations table it is possible to add review annotations to the source where the violation occurred. This can be an effective way to mark target files for further review. Just right-click on any violation in the list and select Mark review. PMD will insert a review annotation to the Java source right above the violation line itself. The review annotation should look like this:


// @PMD:REVIEWED:MethodNamingConventions: by Levent Gurses on 3/28/04 5:04 PM


Review annotations can be removed anytime by right-clicking QA Project and selecting PMD | Clear violations reviews. Similarly, PMD Violations can be cleaned-up by right-clicking QA Project and selecting PMD | Clear PMD Violations.

Finding Cut and Paste Code(CPD):

Repeated (Cut & Paste) code generally indicates poor planning or team coordination. Therefore, refactoring classes with repeating code should be given a high priority. PMD can help identify these classes by scanning the code in a way similar to PMD violation checks. The number of lines of similarity (the metrics used by PMD to match code patterns) is 25 by default and can be set in PMD’s Preferences page.

  1. In Package Explorer right-click on QA Project and select PMDFind Suspect Cut And Paste
  2. PMD creates a folder reportsunder QA Projectand stores the result text file
  3. Select WindowShow ViewNavigator
  4. In Navigator view click on QA Projectand then on the reportsfolder
  5. The report file cpd-report.txtshould look like this:

We can generate two types of reports from PMD after integrating PMD dependencies with Maven and building the project.

Below are the dependencies for Maven Pom.xml file for PMD.

Working POM confiiguration


Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

Your email address will not be published. Required fields are marked *