Java PMD. How To Check Java Source Code With PMD

Oleksii Dushenin
3 min readJul 28, 2021

--

In a How To Format Your Java Code With CheckStyle post, CheckStyle was introduced as a first possible step of a static code analysis process in the CI/CD pipeline. The next step is to use a tool to find common programming flaws. For this purpose, Java PMD can be used. In this post, we will see how to add PMD to the project.

PMD

Java PMD is a static code analysis tool that is used to find common programming flaws.

Flaws are grouped into different categories:

  • Best Practices
  • Code Style
  • Design
  • Documentation
  • Error Prone
  • Multithreading
  • Performance
  • Security
  • Additional rulesets

Each category has a set of rules with a provided priority. For instance, AbstractClassWithoutAbstractMethod is the Best Practices rule with Medium (3) priority.

Java PMD Usage Example

In this post, a Gradle project will be used as an example for PMD usage. The initial project can be created the same way as it was done for the How To Format Your Java Code With CheckStyle post.

Let’s create a class that is wrong from the PMD perspective.

package com.datamify.development.pmd;public class Action {    public void doSomething() {
Long notUsedLong = 5L;
int i = 7;
try {
System.out.println(i + 2);
} catch (Exception e) {
} }}}

In build.gradle PMD plugin has to be used.

plugins { 
id 'pmd'
}

The next configuration is specified:

pmd { 
consoleOutput = true
toolVersion = "6.21.0"
rulesMinimumPriority = 5
}

Using this configuration, we use all possible PMD checks.

The full list of options can be found in PMD plugin documentation. For instance:

  • fail build if number of errors exceeds predefined value
  • use only major rules
  • use only a subset of categories

Let’s build the code.

gradlew clean build

The build fails with the next message:

> Task :pmdMain C:\Users\Oleksii\Documents\datamify\projects\development-pmd\src\main\java\com\datamify\development\pmd\Action.java:6: Found 'DU'-anomaly for variable 'notUsedLong' (lines '6'-'15'). C:\Users\Oleksii\Documents\datamify\projects\development-pmd\src\main\java\com\datamify\development\pmd\Action.java:11: Avoid empty catch blocks > Task :pmdMain FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':pmdMain'. > 2 PMD rule violations were found. See the report at: file:///C:/Users/Oleksii/Documents/datamify/projects/development-pmd/build/reports/pmd/main.html

Following the link, we can see the PMD report.

To fix the build next actions can be done:

  • remove unused notUsedLong variable
  • fix an empty catch block

Summary

In this post, PMD was used to validate the source code based on common programming mistakes. Based on project needs, you can select the appropriate categories, rules, and priorities. It can be the next step in CI/CD pipeline. The check will be done automatically. As a result, the review process will be slightly easier. The reviewer can focus on business logic and not on typical mistakes.

The source code is available at Github.

Originally published at https://datamify.com on July 28, 2021.

--

--

Oleksii Dushenin
Oleksii Dushenin

Written by Oleksii Dushenin

IT Consultant with 8+ years of industry experience — https://datamify.com/

No responses yet