Skip to content

Add or improve existing PMD rules

David Renz edited this page Jun 7, 2016 · 34 revisions

PMD is a Framework and a Maven project written in Java and composed of different modules. Beside some general modules like the pmd-core module, there are modules for every supported language, for example pmd-apex. This modules vary in complexity. Some of them just offer a simple Copy/Paste Detector (CPD), like pmd-perl. Others This modules are all designed the same. A Parser parses the source code into a abstract syntax tree (AST), every generated node have to be represented as a Java class. The Apex-module implements the Apex parser used by the Force.com IDE to parse source code to ASTs. Based on this ASTs and their nodes it is possible to apply rules. The Avoid-deeply-nested-if-statements rule for example checks if there are if-statement nodes with more than one if-statement node above them in the tree. If that is true the rule adds a violation. Currently the Apex-module of PMD offers a few Apex-specific rules, like the Avoid-SOQL-In-Loops rule and Java rules, we liked in general and we were able to port with the information the Apex-parser provides.

If you want to add rules that work similarly to the ones we already have you will have to extend PMD itself. Those are the steps you need to do:

  1. Setup development environment

  2. Add a new rule for Apex PMD

  3. Build PMD and test your new rule

  4. Commit and Pull Request the new rule

Clone this wiki locally