Friday, November 21, 2008

PMD

Introduction

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

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

In its current version, PMD comes packaged with 149 rules in 19 rulesets. Most of these rules can be parameterized at runtime by supplying properties or parameters. The standard package offers many well-thought rules. In addition users also have the ability to add their own rules for particular coding convention or quality metrics. Here are some of the rules distributed with PMD....

  1. EmptyFinalizer - If the finalize() method is empty, then it does not need to exist.
  2. EmptyFinallyBlock - Avoid empty finally blocks - these can be deleted.
  3. UnnecessaryReturn - Avoid unnecessary return statements
  4. OnlyOneReturn - A method should have only one exit point, and that should be the last statement in the method.
  5. CyclomaticComplexity - Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
  6. TooManyFields - Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.
  7. LongVariable - Detects when a field, formal or local variable is declared with a long name.
  8. NoPackage - Detects when a class or interface does not have a package definition.
Advantages of PMD
  1. Automates the code review process
  2. PMD is a static code analyzer capable of automatically detecting a wide range of potential defects and unsafe or non-optimized code. While other tools, such as Checkstyle, can verify that coding conventions and standards are respected, PMD focuses on preemptive defect detection.
  3. CPD (the Copy/Paste Detector) is an add-on to PMD that uses a clever set of algorithms to find duplicated code.
  4. A static Java code analyzer that includes lots of standard rules and supports the ability to write custom rules.

Install PMD plugin for Eclipse

  1. Start Eclipse and open a project
  2. Select "Help"->"Software Updates"->"Find and Install"
  3. Click "Next", then click "New remote site"
  4. Enter "PMD" into the Name field and "http://pmd.sf.net/eclipse" into the URL field
  5. Click through the rest of the dialog boxes to install the plugin
  6. Alternatively, you can download the latest zip file and follow the above procedures except for using "New local site" and browsing to the downloaded zip file.

  1. To configure PMD, select "Windows"->"Preferences", then select PMD.

  1. To run PMD, right-click on a project node and select "PMD"->"Check code with PMD".

  1. To run the duplicate code detector, right-click on a project node and select "PMD"->"Find suspect cut and paste". The report will be placed in a "reports" directory in a file called "cpd-report.txt".

  1. To find additional help for other features, please read included help by selecting Help->Help Contents and browse the "How to..." section in the "PMD Plugin Documentation" book.

  1. After installing an update, if you get an Exception such as "java.lang.RuntimeException: Couldn't find that class xxxxx", try deleting the ruleset.xml file in the .metadata/plugins/net.sourceforge.pmd.eclipse directory in your workspace.

  1. To get Eclipse to not flag the @SuppressWarnings("PMD") annotation, look under the menu headings Java -> Compiler -> Errors/Warnings -> Annotations -> Unhandled Warning Token.

Configuring Rules for PMD
  1. Click on Eclipse project -> properties-->PMD -->Click on “Enable PMD”
  2. Check the checkbox containing the Text “Use the rueset confgured”
  3. Select the ruleset configuration file by clicking on “Browse” button.
  4. download pmd_rules.xml (see the attached) and click on “OK” button.

Checking for Violations
  1. Open any Java file or package in eclipse --> right click select PMD -->Check Code with PMD.
  2. All the violations would be marked within the Editor itself.
  3. Open the “Violation Overview” tab and you will see the 5 types of violation: -
    1. Priority-1 – Error high
    2. Priority-2 – Error
    3. Priority-3 – Warning high
    4. Priority-4 – Warning
    5. Priority-5 – information
Integration with Build Scripts

PMD Integrates very well with ANT and Maven.
It even provides the option to extend PMD Report formatters and customize as per the individual needs.

References

http://pmd.sourceforge.net/
http://pmd.sourceforge.net/integrations.html

No comments: