Interface Node
-
- All Superinterfaces:
Reportable
- All Known Subinterfaces:
AntlrNode<N>
,GenericNode<N>
,JjtreeNode<N>
,RootNode
,ScopedNode
,TextAvailableNode
- All Known Implementing Classes:
AbstractJjtreeNode
,AbstractNode
,BaseAntlrErrorNode
,BaseAntlrInnerNode
,BaseAntlrNode
,BaseAntlrTerminalNode
,PlainTextLanguage.PlainTextFile
public interface Node extends Reportable
Root interface for all AST nodes. This interface provides only the API shared by all AST implementations in PMD language modules. This includes for now:- Tree traversal methods:
getParent()
,getIndexInParent()
,getChild(int)
, andgetNumChildren()
. These four basic operations are used to implement more specific traversal operations, likefirstChild(Class)
, andNodeStream
s. - The API used to describe nodes in a form understandable by XPath expressions:
getXPathNodeName()
,getXPathAttributesIterator()
- Location metadata: eg
getBeginLine()
,getBeginColumn()
- An extensible metadata store:
getUserMap()
Every language implementation must publish a sub-interface of Node which serves as a supertype for all nodes of that language (e.g. pmd-java provides JavaNode, pmd-apex provides ApexNode, etc.). It is assumed in many places that the
getChild(int)
andgetParent()
method return an instance of this sub-interface. For example, no JSP node should have a Java node as its child. Embedding nodes from different languages will not be done via these methods, and conforming implementations should ensure that every node returned by these methods are indeed of the same type. Possibly, a type parameter will be added to the Node interface in 7.0.0 to enforce it at compile-time.
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<Node>
COORDS_COMPARATOR
Compares nodes according to their location in the file.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default <P,R>
RacceptVisitor(AstVisitor<? super P,? extends R> visitor, P data)
Calls back the visitor's visit method corresponding to the runtime type of this Node.default NodeStream<? extends Node>
ancestors()
Returns a node stream containing all the strict ancestors of this node, in innermost to outermost order.default <R extends Node>
NodeStream<R>ancestors(Class<? extends R> rClass)
Returns the ancestor stream of this node filtered by the given node type.default NodeStream<? extends Node>
ancestorsOrSelf()
Returns a node stream containing this node and its ancestors.default NodeStream<? extends Node>
asStream()
Returns a node stream containing only this node.default NodeStream<? extends Node>
children()
Returns a node stream containing all the children of this node.default <R extends Node>
NodeStream<R>children(Class<? extends R> rClass)
Returns a node stream of the children of this node that are of the given type.default int
compareLocation(Node other)
Compare the coordinates of this node with the other one as if withCOORDS_COMPARATOR
.default NodeStream.DescendantNodeStream<? extends Node>
descendants()
Returns a node stream containing all the descendants of this node.default <R extends Node>
NodeStream.DescendantNodeStream<R>descendants(Class<? extends R> rClass)
Returns a node stream of the descendants of this node that are of the given type.default NodeStream.DescendantNodeStream<? extends Node>
descendantsOrSelf()
Returns a node stream containing this node, then all its descendants.default List<Node>
findChildNodesWithXPath(String xpathString)
Deprecated.This is very inefficient and should not be used in new code.default <T extends Node>
List<T>findChildrenOfType(Class<? extends T> childType)
Deprecated.Use node stream methods:node.children(childType).toList()
.default <T extends Node>
List<T>findDescendantsOfType(Class<? extends T> targetType)
Deprecated.Use node stream methods:node.descendants(targetType).toList()
.default <T extends Node>
List<T>findDescendantsOfType(Class<? extends T> targetType, boolean crossFindBoundaries)
Deprecated.Use node stream methods:node.descendants(targetType).crossFindBoundaries(b).toList()
.default <R extends Node>
@Nullable RfirstChild(Class<? extends R> rClass)
Returns the first child of this node that has the given type.default AstInfo<? extends RootNode>
getAstInfo()
Returns theAstInfo
for this root node.default int
getBeginColumn()
Gets the column offset from the start of the begin line where the token's region beginsdefault int
getBeginLine()
Gets the line where the token's region beginsNode
getChild(int index)
Returns the child of this node at the given index.default int
getEndColumn()
Gets the column offset from the start of the end line where the token's region endsdefault int
getEndLine()
Gets the line where the token's region endsdefault @Nullable Node
getFirstChild()
Returns the first child of this node, or null if it doesn't exist.default <T extends Node>
TgetFirstChildOfType(Class<? extends T> childType)
Deprecated.default <T extends Node>
TgetFirstDescendantOfType(Class<? extends T> descendantType)
Deprecated.Use node stream methods:node.descendants(targetType).first()
.default <T extends Node>
TgetFirstParentOfType(Class<? extends T> parentType)
Deprecated.Use node stream methods:node.ancestors(parentType).first()
default String
getImage()
Deprecated.Should be replaced with methods that have more specific names in node classes.int
getIndexInParent()
Returns the index of this node in its parent's children.default LanguageVersion
getLanguageVersion()
Returns the language version of this node.default @Nullable Node
getLastChild()
Returns the first last of this node, or null if it doesn't exist.default @Nullable Node
getNextSibling()
Returns the next sibling of this node, or null if it does not exist.default Node
getNthParent(int n)
Deprecated.Use node stream methods:node.ancestors().get(n-1)
int
getNumChildren()
Returns the number of children of this node.Node
getParent()
Returns the parent of this node, or null if this is the root of the tree.default <T extends Node>
List<T>getParentsOfType(Class<? extends T> parentType)
Deprecated.Use node stream methods:node.ancestors(parentType).toList()
.default @Nullable Node
getPreviousSibling()
Returns the previous sibling of this node, or null if it does not exist.default FileLocation
getReportLocation()
Returns the location at which this element should be reported.default @NonNull RootNode
getRoot()
Returns the root of the tree this node is declared in.default @NonNull TextDocument
getTextDocument()
Returns the text document from which this tree was parsed.TextRegion
getTextRegion()
Returns a region of text delimiting the node in the underlying text document.DataMap<DataMap.DataKey<?,?>>
getUserMap()
Returns a data map used to store additional information on this node.default Iterator<Attribute>
getXPathAttributesIterator()
Returns an iterator enumerating all the attributes that are available from XPath for this node.String
getXPathNodeName()
Gets the name of the node that is used to match it with XPath queries.default <T extends Node>
booleanhasDescendantOfType(Class<? extends T> type)
Deprecated.Use node stream methods:node.descendants(targetType).nonEmpty()
.default boolean
hasImageEqualTo(String image)
Deprecated.SeegetImage()
default boolean
isFindBoundary()
Returns true if this node is considered a boundary by traversal methods.
-
-
-
Field Detail
-
COORDS_COMPARATOR
static final Comparator<Node> COORDS_COMPARATOR
Compares nodes according to their location in the file. Note that this comparator is not consistent with equals (seeComparator
) as some nodes have the same location.
-
-
Method Detail
-
getImage
@Deprecated default String getImage()
Deprecated.Should be replaced with methods that have more specific names in node classes.Returns a string token, usually filled-in by the parser, which describes some textual characteristic of this node. This is usually an identifier, but you should check that using the Designer. On most nodes though, this method returnsnull
.
-
hasImageEqualTo
@Deprecated default boolean hasImageEqualTo(String image)
Deprecated.SeegetImage()
Returns true if this node's image is equal to the given string.- Parameters:
image
- The image to check
-
compareLocation
default int compareLocation(Node other)
Compare the coordinates of this node with the other one as if withCOORDS_COMPARATOR
. The result is useless if both nodes are not from the same tree.- Parameters:
other
- Other node- Returns:
- A positive integer if this node comes AFTER the other, 0 if they have the same position, a negative integer if this node comes BEFORE the other
-
getReportLocation
default FileLocation getReportLocation()
Returns the location at which this element should be reported.Use this instead of
Reportable.getBeginColumn()
/Reportable.getBeginLine()
, etc. This is not necessarily the exact boundaries of the node in the text. Nodes that can provide exact position information do so using aTextRegion
, by implementingTextAvailableNode
.Use this instead of
getBeginColumn()
/getBeginLine()
, etc.- Specified by:
getReportLocation
in interfaceReportable
-
getTextRegion
TextRegion getTextRegion()
Returns a region of text delimiting the node in the underlying text document. This does not necessarily match thereport location
.
-
getBeginLine
default int getBeginLine()
Description copied from interface:Reportable
Gets the line where the token's region begins- Specified by:
getBeginLine
in interfaceReportable
-
getBeginColumn
default int getBeginColumn()
Description copied from interface:Reportable
Gets the column offset from the start of the begin line where the token's region begins- Specified by:
getBeginColumn
in interfaceReportable
-
getEndLine
default int getEndLine()
Description copied from interface:Reportable
Gets the line where the token's region ends- Specified by:
getEndLine
in interfaceReportable
-
getEndColumn
default int getEndColumn()
Description copied from interface:Reportable
Gets the column offset from the start of the end line where the token's region ends- Specified by:
getEndColumn
in interfaceReportable
-
isFindBoundary
default boolean isFindBoundary()
Returns true if this node is considered a boundary by traversal methods. Traversal methods such asdescendants()
don't look past such boundaries by default, which is usually the expected thing to do. For example, in Java, lambdas and nested classes are considered find boundaries.Note: This attribute is deprecated for XPath queries. It is not useful for XPath queries and will be removed with PMD 7.0.0.
- Returns:
- True if this node is a find boundary
- See Also:
NodeStream.DescendantNodeStream.crossFindBoundaries(boolean)
-
getNthParent
@Deprecated default Node getNthParent(int n)
Deprecated.Use node stream methods:node.ancestors().get(n-1)
Returns the n-th parent or null if there are less thann
ancestors.getNthParent(1) == jjtGetParent
- Parameters:
n
- how many ancestors to iterate over.- Returns:
- the n-th parent or null.
- Throws:
IllegalArgumentException
- ifn
is negative or zero.
-
getFirstParentOfType
@Deprecated default <T extends Node> T getFirstParentOfType(Class<? extends T> parentType)
Deprecated.Use node stream methods:node.ancestors(parentType).first()
Traverses up the tree to find the first parent instance of type parentType or one of its subclasses.- Type Parameters:
T
- The type you want to find- Parameters:
parentType
- Class literal of the type you want to find- Returns:
- Node of type parentType. Returns null if none found.
-
getParentsOfType
@Deprecated default <T extends Node> List<T> getParentsOfType(Class<? extends T> parentType)
Deprecated.Use node stream methods:node.ancestors(parentType).toList()
. Most usages don't really need a list though, eg you can iterate the node stream insteadTraverses up the tree to find all of the parent instances of type parentType or one of its subclasses. The nodes are ordered deepest-first.- Type Parameters:
T
- The type you want to find- Parameters:
parentType
- Class literal of the type you want to find- Returns:
- List of parentType instances found.
-
findChildrenOfType
@Deprecated default <T extends Node> List<T> findChildrenOfType(Class<? extends T> childType)
Deprecated.Use node stream methods:node.children(childType).toList()
. Most usages don't really need a list though, eg you can iterate the node stream insteadTraverses the children to find all the instances of type childType or one of its subclasses.- Parameters:
childType
- class which you want to find.- Returns:
- List of all children of type childType. Returns an empty list if none found.
- See Also:
if traversal of the entire tree is needed.
-
findDescendantsOfType
@Deprecated default <T extends Node> List<T> findDescendantsOfType(Class<? extends T> targetType)
Deprecated.Use node stream methods:node.descendants(targetType).toList()
. Most usages don't really need a list though, eg you can iterate the node stream insteadTraverses down the tree to find all the descendant instances of type descendantType without crossing find boundaries.- Parameters:
targetType
- class which you want to find.- Returns:
- List of all children of type targetType. Returns an empty list if none found.
-
findDescendantsOfType
@Deprecated default <T extends Node> List<T> findDescendantsOfType(Class<? extends T> targetType, boolean crossFindBoundaries)
Deprecated.Use node stream methods:node.descendants(targetType).crossFindBoundaries(b).toList()
. Most usages don't really need a list though, eg you can iterate the node stream insteadTraverses down the tree to find all the descendant instances of type descendantType.- Parameters:
targetType
- class which you want to find.crossFindBoundaries
- iffalse
, recursion stops for nodes for whichisFindBoundary()
istrue
- Returns:
- List of all matching descendants
-
getFirstChildOfType
@Deprecated default <T extends Node> T getFirstChildOfType(Class<? extends T> childType)
Deprecated.Traverses the children to find the first instance of type childType.- Parameters:
childType
- class which you want to find.- Returns:
- Node of type childType. Returns
null
if none found. - See Also:
if traversal of the entire tree is needed.
-
getFirstDescendantOfType
@Deprecated default <T extends Node> T getFirstDescendantOfType(Class<? extends T> descendantType)
Deprecated.Use node stream methods:node.descendants(targetType).first()
.Traverses down the tree to find the first descendant instance of type descendantType without crossing find boundaries.- Parameters:
descendantType
- class which you want to find.- Returns:
- Node of type descendantType. Returns
null
if none found.
-
hasDescendantOfType
@Deprecated default <T extends Node> boolean hasDescendantOfType(Class<? extends T> type)
Deprecated.Use node stream methods:node.descendants(targetType).nonEmpty()
.Finds if this node contains a descendant of the given type without crossing find boundaries.- Parameters:
type
- the node type to search- Returns:
true
if there is at least one descendant of the given type
-
findChildNodesWithXPath
@Deprecated default List<Node> findChildNodesWithXPath(String xpathString)
Deprecated.This is very inefficient and should not be used in new code. PMD 7.0.0 will remove support for this method.Returns all the nodes matching the xpath expression.- Parameters:
xpathString
- the expression to check- Returns:
- List of all matching nodes. Returns an empty list if none found.
-
getUserMap
DataMap<DataMap.DataKey<?,?>> getUserMap()
Returns a data map used to store additional information on this node.- Returns:
- The user data map of this node
- Since:
- 6.22.0
-
getTextDocument
default @NonNull TextDocument getTextDocument()
Returns the text document from which this tree was parsed. This means, that the whole file text is in memory while the AST is.- Returns:
- The text document
-
getParent
Node getParent()
Returns the parent of this node, or null if this is the root of the tree.- Returns:
- The parent of this node
- Since:
- 6.21.0
-
getChild
Node getChild(int index)
Returns the child of this node at the given index.- Throws:
IndexOutOfBoundsException
- if the index is negative or greater thangetNumChildren()
.- Since:
- 6.21.0
-
getNumChildren
int getNumChildren()
Returns the number of children of this node.- Since:
- 6.21.0
-
getIndexInParent
int getIndexInParent()
Returns the index of this node in its parent's children. If this node is a root node, returns -1.- Returns:
- The index of this node in its parent's children
- Since:
- 6.21.0
-
acceptVisitor
default <P,R> R acceptVisitor(AstVisitor<? super P,? extends R> visitor, P data)
Calls back the visitor's visit method corresponding to the runtime type of this Node. This should usually be preferred to calling avisit
method directly (usually the only calls to those are in the implementations of thisacceptVisitor
method).- Type Parameters:
R
- Return type of the visitorP
- Parameter type of the visitor- Parameters:
visitor
- Visitor to dispatchdata
- Parameter to the visit- Returns:
- What the visitor returned. If this node doesn't recognize
the type of the visitor, returns
visitor.cannotVisit(this, data)
. - Since:
- 7.0.0
-
getXPathNodeName
String getXPathNodeName()
Gets the name of the node that is used to match it with XPath queries.- Returns:
- The XPath node name
-
getXPathAttributesIterator
default Iterator<Attribute> getXPathAttributesIterator()
Returns an iterator enumerating all the attributes that are available from XPath for this node.- Returns:
- An attribute iterator for this node
-
getFirstChild
default @Nullable Node getFirstChild()
Returns the first child of this node, or null if it doesn't exist.- Since:
- 7.0.0
-
getLastChild
default @Nullable Node getLastChild()
Returns the first last of this node, or null if it doesn't exist.- Since:
- 7.0.0
-
getPreviousSibling
default @Nullable Node getPreviousSibling()
Returns the previous sibling of this node, or null if it does not exist.- Since:
- 7.0.0
-
getNextSibling
default @Nullable Node getNextSibling()
Returns the next sibling of this node, or null if it does not exist.- Since:
- 7.0.0
-
asStream
default NodeStream<? extends Node> asStream()
Returns a node stream containing only this node.NodeStream.of(Node)
is a null-safe version of this method.- Returns:
- A node stream containing only this node
- Since:
- 7.0.0
- See Also:
NodeStream.of(Node)
-
children
default NodeStream<? extends Node> children()
Returns a node stream containing all the children of this node. This method does not provide much type safety, you'll probably want to usechildren(Class)
.- Since:
- 7.0.0
- See Also:
NodeStream.children(Class)
-
descendants
default NodeStream.DescendantNodeStream<? extends Node> descendants()
Returns a node stream containing all the descendants of this node. SeeNodeStream.DescendantNodeStream
for details.- Returns:
- A node stream of the descendants of this node
- Since:
- 7.0.0
- See Also:
NodeStream.descendants()
-
descendantsOrSelf
default NodeStream.DescendantNodeStream<? extends Node> descendantsOrSelf()
Returns a node stream containing this node, then all its descendants. SeeNodeStream.DescendantNodeStream
for details.- Returns:
- A node stream of the whole subtree topped by this node
- Since:
- 7.0.0
- See Also:
NodeStream.descendantsOrSelf()
-
ancestors
default NodeStream<? extends Node> ancestors()
Returns a node stream containing all the strict ancestors of this node, in innermost to outermost order. The returned stream doesn't contain this node, and is empty if this node has no parent.- Returns:
- A node stream of the ancestors of this node
- Since:
- 7.0.0
- See Also:
NodeStream.ancestors()
-
ancestorsOrSelf
default NodeStream<? extends Node> ancestorsOrSelf()
Returns a node stream containing this node and its ancestors. The nodes of the returned stream are yielded in a depth-first fashion.- Returns:
- A stream of ancestors
- Since:
- 7.0.0
- See Also:
NodeStream.ancestorsOrSelf()
-
children
default <R extends Node> NodeStream<R> children(Class<? extends R> rClass)
Returns a node stream of the children of this node that are of the given type.- Type Parameters:
R
- Type of node the returned stream should contain- Parameters:
rClass
- Type of node the returned stream should contain- Returns:
- A new node stream
- Since:
- 7.0.0
- See Also:
NodeStream.children(Class)
-
firstChild
default <R extends Node> @Nullable R firstChild(Class<? extends R> rClass)
Returns the first child of this node that has the given type. Returns null if no such child exists.If you want to process this element as a node stream, use
asStream().firstChild(rClass)
instead, which returns a node stream.- Type Parameters:
R
- Type of the child to find- Parameters:
rClass
- Type of the child to find- Returns:
- A child, or null
- Since:
- 7.0.0
-
descendants
default <R extends Node> NodeStream.DescendantNodeStream<R> descendants(Class<? extends R> rClass)
Returns a node stream of the descendants of this node that are of the given type. SeeNodeStream.DescendantNodeStream
for details.- Type Parameters:
R
- Type of node the returned stream should contain- Parameters:
rClass
- Type of node the returned stream should contain- Returns:
- A new node stream
- Since:
- 7.0.0
- See Also:
NodeStream.descendants(Class)
-
ancestors
default <R extends Node> NodeStream<R> ancestors(Class<? extends R> rClass)
Returns the ancestor stream of this node filtered by the given node type.- Type Parameters:
R
- Type of node the returned stream should contain- Parameters:
rClass
- Type of node the returned stream should contain- Returns:
- A new node stream
- Since:
- 7.0.0
- See Also:
NodeStream.ancestors(Class)
-
getRoot
default @NonNull RootNode getRoot()
Returns the root of the tree this node is declared in.- Since:
- 7.0.0
-
getLanguageVersion
default LanguageVersion getLanguageVersion()
Returns the language version of this node.
-
-