Chapter 22. Preemptive Error Detection with PMD[19]

PMD and Static Code Analysis

PMD is a static code analysis tool, capable of automatically detecting a wide range of potential defects and unsafe or nonoptimized code. Whereas other tools, such as Checkstyle (see Chapter 21), can verify that coding conventions and standards are respected, PMD focuses more on preemptive defect detection. It comes with a rich and highly configurable set of rules, and you can easily configure which particular rules should be used for a given project. PMD integrates well with IDEs such as Eclipse and NetBeans, and it also fits well into the build process thanks to its smooth integration with Ant and Maven.

Using PMD in Eclipse

PMD is designed to integrate well into a developer’s work environment. Plug-ins exist for the principal IDEs, and they are the most productive and convenient way for a developer to use PMD. Plug-ins allow almost real-time code verification—they raise issues whenever you save the source code.

Installing the PMD Plug-In

The easiest way to install and use PMD under Eclipse is to use the remote update site, at http://pmd.sf.net/eclipse. You do this in the usual way:

  1. Open the Help→Software Updates→Find and Install menu.

  2. Click Next, and choose New Remote Site.

  3. Now enter the URL of the remote site (http://pmd.sf.net/eclipse), and an appropriate name such as “PMD.”

  4. Make sure that you have the PMD site checked in the “Sites to include in search” window, and click Finish. Then just go through the ...

Get Java Power Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.