XPath rules view the AST as an XML-like DOM, which is what the XPath language is
defined on. Concretely, this means:
Every AST node is viewed as an XML element
The element has for local name the value of getXPathNodeName
for the given node
Some Java getters are exposed as XML attributes on those elements
This means, that documentation for attributes can be found in our Javadocs. For
example, the attribute @SimpleName of the Java node EnumDeclaration is backed
by the Java getter getSimpleName.
To represent attributes, we must map Java values to XPath Data Model (XDM)
values. In the following table we refer to the type conversion function as conv, a function from Java types
to XDM types.
Java type T
XSD type conv(T)
int
xs:integer
long
xs:integer
double
xs:decimal
float
xs:decimal
boolean
xs:boolean
String
xs:string
Character
xs:string
Enum<E>
xs:string (uses Object::toString)
Collection<E>
conv(E)* (a sequence type)
The same conv function is used to translate rule property values to XDM values.
Additionaly, PMD’s own net.sourceforge.pmd.lang.document.Chars is also translated to a xs:string
Returns true if the runtime type of the AST node is a subtype of the given class. Contrary to typeIs, this tests the type of the AST node. For example, the AST node for a literal (e.g. 5d) has type ASTNumericLiteral, and this function will ignore the static type of the expression (double)
Returns true if the context node's static Java type is a subtype of the given type. This tests for the resolved type of the Java construct, not the type of the AST node. For example, the AST node for a literal (e.g. 5d) has type ASTNumericLiteral, however this function will compare the type of the literal (eg here, double) against the argument.
Matches formal parameters of type String[] (including vararg parameters)
//VariableId[pmd-java:typeIs("java.lang.List")]
Matches variable declarators of type List or any of its subtypes (including e.g. ArrayList)
typeIsExactly
Tests a node's static type, ignoring subtypes
pmd-java:typeIsExactly(xs:string) as xs:boolean✏️️
Returns true if the context node's static type is exactly the given type. In particular, returns false if the context node's type is a subtype of the given type.
Returns the value of the metric as evaluated on the context node. If the metric cannot be computed on that node, returns an empty sequence (which is falsy).
Remarks
Parameters
metricKey as xs:string
The name of a metric in JavaMetrics (or an alias thereof).
Examples
//ClassDeclaration[metric('NCSS') > 200]
//MethodDeclaration[metric('CYCLO') > 10 and metric('NCSS') > 20]
//TypeParameter[metric('idontexist') > 50]
Error: no such metric
hasAnnotation
Tests whether an annotation is present on the node
pmd-java:hasAnnotation(xs:string) as xs:boolean✏️️
Returns true if the node has an annotation with the given qualified name
Remarks
The context node must be an Annotatable, otherwise this returns false
Matches method declarations that have a 'public' modifier, explicit or implicit. For example, this would match methods in interfaces, which implicitly have the modifier. Use the explicitModifiers function if you don't want the implicit part. Also note that @Visibility = 'public' is a better use of the API, in this particular instance.
Uses an TypeTestUtil.InvocationMatcher to test the method signature called by the context node. The format of the parameter is described on that class.
Remarks
The context node must be an InvocationNode, otherwise this returns false