Movatterモバイル変換


[0]ホーム

URL:


 
» Writing XPath rules Edit on GitHub

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. Seealsothe 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.

Seethe Saxonica documentationfor an introduction to new features in XPath 3.1.

The propertyversion ofXPathRule is deprecated andhas 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 isdefined 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. Forexample, the attribute@SimpleName of the Java nodeEnumDeclaration is backedby the Java gettergetSimpleName.

Value conversion

To represent attributes, we must map Java values toXPath Data Model (XDM)values. In the following table we refer to the type conversion function asconv, a function from Java typesto XDM types.

Java typeTXSD typeconv(T)
intxs:integer
longxs:integer
doublexs:decimal
floatxs:decimal
booleanxs:boolean
Stringxs:string
Characterxs:string
Enum<E>xs:string (usesObject::toString)
Collection<E>conv(E)* (a sequence type)

The sameconv function is used to translate rule property values to XDM values.

Additionally, PMD’s ownnet.sourceforge.pmd.lang.document.Chars is also translated to axs:string

Rule properties

SeeDefining rule properties

PMD extension functions

PMD provides some language-specific XPath functions to access semanticinformation from the AST.

The namespace of custom PMD functions must be explicitly mentioned.

All languages

Functions available to all languages are in the namespacepmd.

Function nameDescription (click for details)
fileNameReturns 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 calledFoo.xml.
startLineReturns 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.
endLineReturns 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.
startColumnReturns 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
endColumnReturns 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 namespacepmd-java.

Function nameDescription (click for details)
nodeIsTests 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 packagenet.sourceforge.pmd.lang.java.ast, without the 'AST' prefix
Examples
//*[pmd-java:nodeIs("Expression")]
Matches all nodes that implementASTExpression
//*[pmd-java:nodeIs("TypeDeclaration")]
Matches all nodes that implementASTTypeDeclaration
//*[pmd-java:nodeIs("Foo")]
Runtime error, there's no class ASTFoo in the package
typeIsTests 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 aTypeNode
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 typeString[] (including vararg parameters)
//VariableId[pmd-java:typeIs("java.lang.List")]
Matches variable declarators of typeList or any of its subtypes (including e.g.ArrayList)
typeIsExactlyTests 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 aTypeNode
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 typeList (but not e.g.ArrayList)
metricComputes 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 inJavaMetrics (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
hasAnnotationTests 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 anAnnotatable, 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
modifiersProduce 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 ongetEffectiveModifiers.
Remarks
The context node must be anModifierOwner, 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 theexplicitModifiers 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.
explicitModifiersProduce 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 ongetExplicitModifiers.
Remarks
The context node must be anModifierOwner, otherwise this returns an empty sequence
Examples
//MethodDeclaration[pmd-java:explicitModifiers() = "public"]
Matches method declarations that have an explicit 'public' modifier.
matchesSigMatches the signature called by a method or constructor call

pmd-java:matchesSig(xs:string) as xs:boolean

Uses anTypeTestUtil.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 anInvocationNode, otherwise this returns false
Parameters
sig as xs:string
A signature, in the format described onTypeTestUtil.InvocationMatcher
Examples
//MethodCall[pmd-java:matchesSig("_#equals(java.lang.Object)")]
Matches calls to the methodequals on any receiver type
//MethodCall[pmd-java:matchesSig("java.lang.Enum#equals(java.lang.Object)")]
Matches calls to the methodequals on receiver that is a subtype of Enum
//MethodCall[pmd-java:matchesSig("java.lang.String#toString()")]
Matches calls to the methodtoString on String receivers
//MethodCall[pmd-java:matchesSig("_#_(int,int)")]
Matches calls to any method with 2int parameters (!= argument)
//ConstructorCall[pmd-java:matchesSig("java.util.ArrayList#new(int)")]
Matches constructors calls of ArrayList with a single int parameter

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

©2026 PMD Open Source Project. All rights reserved.
Page last updated: April 2024 (7.2.0)
Site last generated: Jan 30, 2026

PMD                logo


[8]ページ先頭

©2009-2026 Movatter.jp