PMD is a framework to perform code analysis. You can create your own rules tocheck for patterns specific to your codebase, or the coding practices of yourteam.
Before running rules, PMD parses the source file into a data structure called anabstract syntax tree (AST).This tree represents the syntactic structure of thecode, and encodes syntactic relations between source code elements. For instance,in Java, method declarations belong to a class: in the AST, the nodes representingmethod declarations will be descendants of a node representing the declaration oftheir enclosing class. This representation is thus much richer than the originalsource code (which, for a program, is just a chain of characters), or the tokenchain produced by a lexer. For example:
| Sample code (Java) | AST |
|---|---|
| |
Conceptually, PMD rules work bymatching a “pattern” against the AST of afile.Rules explore the AST and find nodes that satisfy some conditions that are characteristicof the specific thing the rule is trying to flag. Rules then report a violation on these nodes.
ASTs are represented by Java classes deriving fromNode.Each PMD language has its own set of such classes, and its own rules about howthese classes relate to one another, based on the grammar of the language. Forexample, all Java AST nodes extendJavaNode.
The structure of the AST can be discovered through
PMD supports two ways to define rules: using anXPath query, or using aJava visitor. XPath rules are much easier to set up, since they’re defineddirectly in your ruleset XML, and are expressive enough for nearly any task.
On the other hand, some parts of PMD’s API are only accessible from Java, e.g.accessing the usages of a declaration. And Java rules allow you to do somecomplicated processing, to which an XPath rule couldn’t scale.
In the end, choosing one strategy or the other depends on the difficulty of whatyour rule does. I’d advise to keep to XPath unless you have no other choice.
Note: Despite that fact, the Java rules are written in Java, any language that PMD supportscan be analyzed. E.g. you can write a Java rule to analyze Apex source code.
New rules must be declared in a ruleset before they’re referenced. This is thecase for both XPath and Java rules. To do this, therule element is used, butinstead of mentioning theref attribute, it mentions theclass attribute,with the implementation class of your rule.
net.sourceforge.pmd.lang.rule.xpath.XPathRule.net.sourceforge.pmd.lang.xml.rule.DomXPathRule.SeeXPath rules in XML for more info.Example for Java rule:
<rulename="MyJavaRule"language="java"message="Violation!"class="com.me.MyJavaRule"><description> Description</description><priority>3</priority></rule>Example for XPath rule:
<rulename="MyXPathRule"language="java"message="Violation!"class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"><description> Description</description><priority>3</priority><properties><propertyname="xpath"><value><![CDATA[//ClassOrInterfaceDeclaration]]></value></property></properties></rule>Note:Since PMD 7, thelanguage attribute is required on allrule elements that declare a new rule. In PMD 6, this was optional, as the base rule classes sometimes set the language implicitly in their constructor.
To learn how to write a rule:
To go further: