GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.pmd-code.org — Your first rule | PMD Source Code Analyzer
Release date: 28-December-2026
Your first ruleIntroduction to rule writing through an example for a XPath rule.
Table of ContentsThis page is a gentle introduction to rule writing, and the Rule Designer. Using the designer is useful both to write Java rules and XPath rules, but it’s more specifically geared towards XPath rules. This page uses a simple XPath rule to illustrate the common workflow. We assume here that you already know what XPath is and how to read basic XPath queries. W3C has a good tutorial here if you don’t (in the context of XML only), and the Saxon documentation features a comprehensive but approachable description of the syntax of XPath expressions. The Rule DesignerThe rule designer is a tool that packs a lot of features to help you develop XPath rules quickly and painlessly. Basically, it allows you to examine the AST of a code snippet and evaluate an XPath expression against it. You can launch it from Command Line as follows: The interface looks like the following: ![]() The zone (2) is the main editor. When you write a code snippet in the code area to the left, you’ll see that the tree to the right will be updated automatically: it’s the AST of the code. Note that the code snippet must be a syntactically valid compilation unit for the language you’ve chosen, e.g. for Java, a compilation unit necessarily has a top-level type declaration. If you select a node in the AST, its specific properties will also be displayed in the panel (1): they’re the XPath attributes of the node. More on that later. The zone (3) is the XPath editor. If you enter an XPath query in that area, it will be evaluated on the current AST and the results will be displayed in the list to the bottom right. Rule development processThe basic development process is straightforward:
Each time you test your rule against a different snippet, it’s a good idea to save it to make test cases. In the following sections, we walk through several examples to refine your rule. A simple ruleLet’s say you want to prevent your coding team from naming variables of type
Examining the AST, you find out that the LocalVariableDeclaration has a VariableId
descendant, whose You can see the XPath result list is updated with the variable declarator.
If you try the query against the following updated snippet though, you can
see that the field declaration id is matched even though it’s not of type You thus refine your XPath expression with an additional predicate, based on your examination of the Type node of the field and local variable declaration nodes. Exporting to XMLYou estimate that your rule is now production ready, and you’d like to use it in your ruleset.
The second button in the toolbar above the XPath editor (Tooltip: You can notice that your XPath expression ends up inside a property of a rule of type XPathRule, which is how XPath rules are implemented. |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |