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.
- Launch Eclipse.
- Navigate to Help| Software Updates| Find and Install…
- Select “Search for new features to install” and click Next
- Click New Remote Site…
- Enter a name (PMD) and the URL http://pmd.sourceforge.net/eclipse
- In Sites to include in searchcheck PMD and click Finish
- In the Search Resultsdialog check PMD for Eclipse 3 3.1.0 and click Next
- Accept the terms of the license agreements and click Next
- Click Finish.
- Wait for Eclipse to download the required jar files, then click Install
- Restart the workbench. This will load the PMD plugin.
- Navigate to Window| Show View | ..
- Select PMD| PMD Violations
- 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)
- Launch Eclipse
- If you have previously created a Java Project, skip to Step 6. Otherwise click File| New| ..
- Select Java Projectand click Next
- Enter a project name (QA Project) and leave everything else in the default state.
- Click Finish. Eclipse will ask if you want to switch to the Java Perspective. Click Yes.
- In the Package Explorer right-click on QA Project and select New| Class
- In the following dialog enter the class name as Yingand click Finish
- A new class Yingis created in project’s default package. Paste the following code into the new class:
- In the Package Explorer right-click on QA Project and select PMD| Check Code With PMD
- Wait for PMD to scan Yingand Yang
- If PMD Violations view is not open navigate to Window| Show View | .. and select PMD | PMD Violations
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.
- In Package Explorer right-click on QA Project and select PMD| Find Suspect Cut And Paste
- PMD creates a folder reportsunder QA Projectand stores the result text file
- Select Window| Show View| Navigator
- In Navigator view click on QA Projectand then on the reportsfolder
- 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.