Movatterモバイル変換


[0]ホーム

URL:


 
» Introduction to writing PMD rules Edit on GitHub

Introduction to writing PMD rules

Writing your own PMD rules
Table of Contents

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.

How rules work: the AST

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
classFooextendsObject{}
└─CompilationUnit└─TypeDeclaration└─ClassDeclaration"Foo"├─ModifierList├─ExtendsList└─ClassType"Object"└─ClassBody

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.

Discovering the AST

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

Writing new rules

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.

XML rule definition

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.

  • For Java rules: this is the concrete class extending AbstractRule (transitively)
  • For XPath rules: this isnet.sourceforge.pmd.lang.rule.xpath.XPathRule.
  • For XPath rules analyzing XML-based languages: this isnet.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.

Resource index

To learn how to write a rule:

To go further:

  • Defining Propertiesdescribes how to make your rules more configurable with rule properties
  • Testing your Rules introducesour testing framework and how you can use it to safeguard the quality ofyour rule

This documentation is written in markdown.
If there is something missing or can be improved, edit this page on github and create a PR: Edit on GitHub

©2025 PMD Open Source Project. All rights reserved.
Page last updated: December 2023 (7.0.0)
Site last generated: Nov 28, 2025

PMD                logo


[8]ページ先頭

©2009-2025 Movatter.jp