GeezerVison is showing docs.pmd-code.org — Writing XPath rules | PMD Source Code Analyzer
Writing XPath rules
This page describes XPath rule support in more details
Table of Contents
This page describes some points of XPath rule support in more details. See
also the tutorial about how to write a first (XPath) rule.
XPath version
PMD uses XPath 3.1 for its XPath rules since PMD 7. Before then, the default version was XPath 1.0,
with opt-in support for XPath 2.0.
See the Saxonica documentation
for an introduction to new features in XPath 3.1.
The property version of XPathRule is deprecated and
has been removed with PMD 7.
DOM representation of ASTs
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
- 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.
Value conversion
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.
Additionally, PMD’s own net.sourceforge.pmd.lang.document.Chars is also translated to a xs:string
Rule properties
See Defining rule properties
PMD extension functions
PMD provides some language-specific XPath functions to access semantic
information from the AST.
The namespace of custom PMD functions must be explicitly mentioned.
All languages
Functions available to all languages are in the namespace pmd.
| Function name |
Description (click for details) |
|
| fileName |
Returns the simple name of the current file |
|
pmd:fileName() as xs:string
- Returns the current simple file name, without path but including the extension.
This can be used to write rules that check file naming conventions.
- Since
- PMD 6.38.0
- Remarks
- The requires the context node to be an element
- Examples
-
//b[pmd:fileName() = 'Foo.xml']
- Matches any
<b> tags in files called Foo.xml.
|
| startLine |
Returns the start line of the given node |
|
pmd:startLine(xs:element) as xs:int
- Returns the line where the node starts in the source file.
Line numbers are 1-based.
- Since
- PMD 6.44.0
- Remarks
- The function is not context-dependent, but takes a node as its first parameter.
- Parameters
-
-
element
as xs:element
- Any element node
- Examples
-
//b[pmd:startLine(.) > 5]
- Matches any
<b> node which starts after the fifth line.
|
| endLine |
Returns the end line of the given node |
|
pmd:endLine(xs:element) as xs:int
- Returns the line where the node ends in the source file.
Line numbers are 1-based.
- Since
- PMD 6.44.0
- Remarks
- The function is not context-dependent, but takes a node as its first parameter.
- Parameters
-
-
element
as xs:element
- Any element node
- Examples
-
//b[pmd:endLine(.) == pmd:startLine(.)]
- Matches any
<b> node which doesn't span more than one line.
|
| startColumn |
Returns the start column of the given node (inclusive) |
|
pmd:startColumn(xs:element) as xs:int
- Returns the column number where the node starts in the source file.
Column numbers are 1-based. The start column is inclusive.
- Since
- PMD 6.44.0
- Remarks
- The function is not context-dependent, but takes a node as its first parameter.
- Parameters
-
-
element
as xs:element
- Any element node
- Examples
-
//b[pmd:startColumn(.) = 1]
- Matches any
<b> node which starts on the first column of a line
|
| endColumn |
Returns the end column of the given node (exclusive) |
|
pmd:endColumn(xs:element) as xs:int
- Returns the column number where the node ends in the source file.
Column numbers are 1-based. The end column is exclusive.
- Since
- PMD 6.44.0
- Remarks
- The function is not context-dependent, but takes a node as its first parameter.
- Parameters
-
-
element
as xs:element
- Any element node
- Examples
-
//b[pmd:startLine(.) = pmd:endLine(.) and pmd:endColumn(.) - pmd:startColumn(.) = 1]
- Matches any
<b> node which spans exactly one character
|
Java
Java functions are in the namespace pmd-java.
| Function name |
Description (click for details) |
|
| nodeIs |
Tests the runtime type of the node instance |
|
pmd-java:nodeIs(xs:string) as xs:boolean
- 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)
- Remarks
- Parameters
-
-
nodeClassName
as xs:string
- Simple name of a class or interface in package
net.sourceforge.pmd.lang.java.ast, without the 'AST' prefix
- Examples
-
//*[pmd-java:nodeIs("Expression")]
- Matches all nodes that implement
ASTExpression
//*[pmd-java:nodeIs("TypeDeclaration")]
- Matches all nodes that implement
ASTTypeDeclaration
//*[pmd-java:nodeIs("Foo")]
- Runtime error, there's no class ASTFoo in the package
|
| typeIs |
Tests a node's static type |
|
pmd-java:typeIs(xs:string) as xs:boolean
- 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.
- Remarks
- The context node must be a
TypeNode
- Parameters
-
-
javaQualifiedName
as xs:string
- The qualified name of a Java class, possibly with pairs of brackets to indicate an array type. Can also be a primitive type name.
- Examples
-
//FormalParameter[pmd-java:typeIs("java.lang.String[]")]
- 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.
- Remarks
- The context node must be a
TypeNode
- Parameters
-
-
javaQualifiedName
as xs:string
- The qualified name of a Java class, possibly with pairs of brackets to indicate an array type. Can also be a primitive type name.
- Examples
-
//VariableId[pmd-java:typeIsExactly("java.lang.List")]
- Matches variable declarators of type
List (but not e.g. ArrayList)
|
| metric |
Computes and returns the value of a metric |
|
pmd-java:metric(xs:string) as xs:decimal?
- 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
- Parameters
-
-
annotationClassName
as xs:string
- Canonical name of an annotation type
- Examples
-
//MethodDeclaration[pmd-java:hasAnnotation("java.lang.Override")]
- Matches all method declarations that are annotated with @Override
|
| modifiers |
Produce the effective modifiers of a node |
|
pmd-java:modifiers() as xs:string*
- Returns a sequence of the effective modifiers of a node as strings. This is documented on
getEffectiveModifiers.
- Remarks
- The context node must be an
ModifierOwner, otherwise this returns an empty sequence
- Examples
-
//MethodDeclaration[pmd-java:modifiers() = "native"]
- Matches native method declarations
//MethodDeclaration[pmd-java:modifiers() = ("native", "static")]
- Matches method declarations that have a 'native' OR a 'static' modifier. This may be counter-intuitive.
//MethodDeclaration[pmd-java:modifiers() = "public"]
- 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.
|
| explicitModifiers |
Produce the explicit modifiers of a node |
|
pmd-java:explicitModifiers() as xs:string*
- Returns a sequence of the explicit modifiers of a node as strings. This is documented on
getExplicitModifiers.
- Remarks
- The context node must be an
ModifierOwner, otherwise this returns an empty sequence
- Examples
-
//MethodDeclaration[pmd-java:explicitModifiers() = "public"]
- Matches method declarations that have an explicit 'public' modifier.
|
| matchesSig |
Matches the signature called by a method or constructor call |
|
pmd-java:matchesSig(xs:string) as xs:boolean
- 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
- Parameters
-
-
sig
as xs:string
- A signature, in the format described on
TypeTestUtil.InvocationMatcher
- Examples
-
//MethodCall[pmd-java:matchesSig("_#equals(java.lang.Object)")]
- Matches calls to the method
equals on any receiver type
//MethodCall[pmd-java:matchesSig("java.lang.Enum#equals(java.lang.Object)")]
- Matches calls to the method
equals on receiver that is a subtype of Enum
//MethodCall[pmd-java:matchesSig("java.lang.String#toString()")]
- Matches calls to the method
toString on String receivers
//MethodCall[pmd-java:matchesSig("_#_(int,int)")]
- Matches calls to any method with 2
int parameters (!= argument)
//ConstructorCall[pmd-java:matchesSig("java.util.ArrayList#new(int)")]
- Matches constructors calls of ArrayList with a single int parameter
|
Kotlin
Functions available for the Kotlin language are in the namespace pmd-kotlin. See also Kotlin support.
| Function name |
Description (click for details) |
|
| typeIs |
Tests a node's resolved type |
|
pmd-kotlin:typeIs(xs:string) as xs:boolean
- Returns true if the context node's resolved type is the given type, or a subtype of it, using the type hierarchy provided by kotlin-type-mapper. Both Kotlin names (e.g.
kotlin.String) and Java names (e.g. java.lang.String) are accepted. Generic type arguments are ignored for comparison.
- Remarks
- Works on PropertyDeclaration, FunctionDeclaration, ClassParameter, FunctionValueParameter, CatchBlock, ForStatement, and DelegationSpecifier nodes. Requires
auxClasspath for type resolution.
- Parameters
-
-
typeName
as xs:string
- A Kotlin or Java qualified type name
- Examples
-
//PropertyDeclaration[pmd-kotlin:typeIs('java.util.Calendar')]
- Matches property declarations whose type is Calendar or a subtype of it
|
| typeIsExactly |
Tests a node's resolved type, ignoring subtypes |
|
pmd-kotlin:typeIsExactly(xs:string) as xs:boolean
- Like typeIs, but only matches the exact declared type, not subtypes. Generic type arguments are ignored for comparison.
- Remarks
- Requires
auxClasspath for type resolution.
- Parameters
-
-
typeName
as xs:string
- A Kotlin or Java qualified type name
- Examples
-
//PropertyDeclaration[pmd-kotlin:typeIsExactly('kotlin.collections.List')]
- Matches property declarations of type List exactly (but not e.g. ArrayList)
|
| hasAnnotation |
Tests whether an annotation is present on the node |
|
pmd-kotlin:hasAnnotation(xs:string) as xs:boolean
- Returns true if the context node is annotated with an annotation whose simple name or fully-qualified name matches the given name.
- Remarks
- Works on FunctionDeclaration, ClassDeclaration, PropertyDeclaration, and ClassParameter.
- Parameters
-
-
name
as xs:string
- Simple or fully-qualified name of an annotation type
- Examples
-
//ClassDeclaration[pmd-kotlin:hasAnnotation('Entity')]
- Matches class declarations annotated with @Entity
//FunctionDeclaration[pmd-kotlin:hasAnnotation('kotlin.Deprecated')]
- Matches function declarations annotated with @Deprecated
|
| modifiers |
Produce the modifiers of a node |
|
pmd-kotlin:modifiers() as xs:string*
- Returns a sequence of modifier keyword strings for the context node. Use the XPath
= operator (which tests sequence membership) to check for a specific modifier.
- Remarks
- Examples
-
//FunctionDeclaration[pmd-kotlin:modifiers() = 'suspend']
- Matches function declarations with a 'suspend' modifier
//ClassDeclaration[pmd-kotlin:modifiers() = ('internal', 'abstract')]
- Matches class declarations with an 'internal' OR an 'abstract' modifier
|
| isNullable |
Tests whether a node's declared type is nullable |
|
pmd-kotlin:isNullable() as xs:boolean
- Returns true if the context node's declared type is nullable (i.e. has a
? suffix in the Kotlin source).
- Remarks
- Works on PropertyDeclaration, FunctionDeclaration, ClassParameter, FunctionValueParameter, CatchBlock, and ForStatement nodes. Requires
auxClasspath for type resolution.
- Examples
-
//PropertyDeclaration[pmd-kotlin:isNullable()]
- Matches property declarations with a nullable type
|
| hasUnresolvedReference |
Tests whether the node's line has an unresolved reference |
|
pmd-kotlin:hasUnresolvedReference() as xs:boolean
- Returns true if the context node contains an unresolved reference on its begin line (i.e. a symbol that kotlin-type-mapper could not resolve). Useful for filtering out false positives in rules that depend on fully-resolved types.
- Remarks
- Only reports presence/absence, not which symbol is unresolved; only checks the context node's begin line, not other lines of a multi-line declaration.
- Examples
-
//PostfixUnaryExpression[not(pmd-kotlin:hasUnresolvedReference())]
- Matches postfix unary expressions whose type could be fully resolved
|
| matchesSig |
Matches the signature called by a call site |
|
pmd-kotlin:matchesSig(xs:string) as xs:boolean
- Returns true if the context node represents a call site whose method signature matches the given signature. Signature format:
ReceiverType#methodName(paramType, ...) with _ as wildcard for the receiver or any single parameter type, and * as wildcard for any parameter list (any number of parameters).
- Remarks
- Requires
auxClasspath for type resolution.
- Parameters
-
-
sig
as xs:string
- A signature in the format described above
- Examples
-
//PostfixUnaryExpression[pmd-kotlin:matchesSig('java.util.Calendar#getInstance()')]
- Matches calls to the static method Calendar.getInstance()
//PostfixUnaryExpression[pmd-kotlin:matchesSig('java.lang.Throwable#printStackTrace(*)')]
- Matches calls to printStackTrace with any number of parameters
|
|