From d1437323559ef906268e36a75a12001cf36ace58 Mon Sep 17 00:00:00 2001
From: Romain Pelisse
From FOLDOC an AST is 'A data structure - representing something which has been parsed, often used as - a compiler or interpreter's internal representation of a - program while it is being optimised and from which code +
From FOLDOC an AST is 'A data structure + representing something which has been parsed, often used as + a compiler or interpreter's internal representation of a + program while it is being optimised and from which code generation is performed'.
In our context, this means that we basically have a tree representation @@ -29,12 +29,12 @@ Writing PMD rules with XPath can be a bit easier than writing rules with Java co And since it's conceptually similar to XML, it can be queried with XPath to find a pattern.
PMD comes with a handy tool that you will love if you want to write an XPath rule.
Designer, runnable from a script in bin/
, is a very simple and useful utility for writing rules.
The basic steps involved in writing XPath rules are these: +
The basic steps involved in writing XPath rules are these:
In a similar way, you can match only local variables with this +
In a similar way, you can match only local variables with this expression
With local variables we need to be more careful. Consider the @@ -82,18 +82,18 @@ Writing PMD rules with XPath can be a bit easier than writing rules with Java co final int one; int two; - { + { int a; } - } + } }]]> -
Local variable declarations will match 'a', since it is a perfectly +
Local variable declarations will match 'a', since it is a perfectly legal Java local variable. Now, a more interesting expression is - to match variables declared in a method, and not on an internal block, + to match variables declared in a method, and not on an internal block, nor in the class. Maybe you'll start with an expression like this:
You'll quickly see that all three local variables are matched. A possible - solution for this is to request that the parent of the local variable + solution for this is to request that the parent of the local variable declaration is the MethodDeclaration node:
Let's consider that we are writing rules for logger. Let's assume we +
Let's consider that we are writing rules for logger. Let's assume we use the Java logging API and we want to find all classes that have more than one logger. The following expression returns all variable declarations whose type is 'Logger'.
- -Finding a class with more than one logger is quite easy now. This + +
Finding a class with more than one logger is quite easy now. This expression matches the classes we are looking for.
- -But let's refine this expression a little bit more. Consider the + +
But let's refine this expression a little bit more. Consider the following class: