Package net.sourceforge.pmd.lang.ast
Interface Node
-
- All Known Subinterfaces:
DFAGraphMethod
,QualifiableNode
,ScopedNode
,SignedNode<N>
- All Known Implementing Classes:
AbstractNode
public interface Node
All AST nodes must implement this interface. It provides basic machinery for constructing the parent and child relationships between nodes.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.util.List<? extends Node>
findChildNodesWithXPath(java.lang.String xpathString)
Returns all the nodes matching the xpath expression.<T> java.util.List<T>
findChildrenOfType(java.lang.Class<T> childType)
Traverses the children to find all the instances of type childType or one of its subclasses.<T> java.util.List<T>
findDescendantsOfType(java.lang.Class<T> targetType)
Traverses down the tree to find all the descendant instances of type descendantType without crossing find boundaries.<T> void
findDescendantsOfType(java.lang.Class<T> targetType, java.util.List<T> results, boolean crossFindBoundaries)
Traverses down the tree to find all the descendant instances of type descendantType.org.w3c.dom.Document
getAsDocument()
Get a DOM Document which contains Elements and Attributes representative of this Node and it's children.int
getBeginColumn()
int
getBeginLine()
DataFlowNode
getDataFlowNode()
int
getEndColumn()
int
getEndLine()
<T> T
getFirstChildOfType(java.lang.Class<T> childType)
Traverses the children to find the first instance of type childType.<T> T
getFirstDescendantOfType(java.lang.Class<T> descendantType)
Traverses down the tree to find the first descendant instance of type descendantType without crossing find boundaries.<T> T
getFirstParentOfAnyType(java.lang.Class<? extends T>... parentTypes)
Gets the first parent that's an instance of any of the given types.<T> T
getFirstParentOfType(java.lang.Class<T> parentType)
Traverses up the tree to find the first parent instance of type parentType or one of its subclasses.java.lang.String
getImage()
Returns a string token, usually filled-in by the parser, which describes some textual characteristic of this node.Node
getNthParent(int n)
Returns the n-th parent or null if there are less thann
ancestors.<T> java.util.List<T>
getParentsOfType(java.lang.Class<T> parentType)
Traverses up the tree to find all of the parent instances of type parentType or one of its subclasses.java.lang.Object
getUserData()
Get the user data associated with this node.java.util.Iterator<Attribute>
getXPathAttributesIterator()
Returns an iterator enumerating all the attributes that are available from XPath for this node.java.lang.String
getXPathNodeName()
Gets the name of the node that is used to match it with XPath queries.boolean
hasDescendantMatchingXPath(java.lang.String xpathString)
Checks whether at least one descendant matches the xpath expression.<T> boolean
hasDescendantOfType(java.lang.Class<T> type)
Finds if this node contains a descendant of the given type without crossing find boundaries.boolean
hasImageEqualTo(java.lang.String image)
Returns true if this node's image is equal to the given string.boolean
isFindBoundary()
Returns true if this node is considered a boundary by traversal methods.void
jjtAddChild(Node child, int index)
This method tells the node to add its argument to the node's list of children.void
jjtClose()
This method is called after all the child nodes have been added.Node
jjtGetChild(int index)
This method returns a child node.int
jjtGetChildIndex()
Gets the index of this node in the children of its parent.int
jjtGetId()
int
jjtGetNumChildren()
Returns the number of children the node has.Node
jjtGetParent()
Returns the parent of this node.void
jjtOpen()
This method is called after the node has been made the current node.void
jjtSetChildIndex(int index)
Sets the index of this node from the perspective of its parent.void
jjtSetParent(Node parent)
Sets the parent of this node.void
remove()
Remove the current node from its parent.void
removeChildAtIndex(int childIndex)
This method tells the node to remove the child node at the given index from the node's list of children, if any; if not, no changes are done.void
setDataFlowNode(DataFlowNode dataFlowNode)
void
setImage(java.lang.String image)
void
setUserData(java.lang.Object userData)
Set the user data associated with this node.
-
-
-
Method Detail
-
jjtOpen
void jjtOpen()
This method is called after the node has been made the current node. It indicates that child nodes can now be added to it.
-
jjtClose
void jjtClose()
This method is called after all the child nodes have been added.
-
jjtSetParent
void jjtSetParent(Node parent)
Sets the parent of this node.- Parameters:
parent
- The parent
-
jjtGetParent
Node jjtGetParent()
Returns the parent of this node.- Returns:
- The parent of the node
-
jjtAddChild
void jjtAddChild(Node child, int index)
This method tells the node to add its argument to the node's list of children.- Parameters:
child
- The child to addindex
- The index to which the child will be added
-
jjtSetChildIndex
void jjtSetChildIndex(int index)
Sets the index of this node from the perspective of its parent. This means: this.jjtGetParent().jjtGetChild(index) == this.- Parameters:
index
- the child index
-
jjtGetChildIndex
int jjtGetChildIndex()
Gets the index of this node in the children of its parent.- Returns:
- The index of the node
-
jjtGetChild
Node jjtGetChild(int index)
This method returns a child node. The children are numbered from zero, left to right.- Parameters:
index
- the child index. Must be nonnegative and less thanjjtGetNumChildren()
.
-
jjtGetNumChildren
int jjtGetNumChildren()
Returns the number of children the node has.
-
jjtGetId
int jjtGetId()
-
getImage
java.lang.String getImage()
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
.
-
setImage
void setImage(java.lang.String image)
-
hasImageEqualTo
boolean hasImageEqualTo(java.lang.String image)
Returns true if this node's image is equal to the given string.- Parameters:
image
- The image to check
-
getBeginLine
int getBeginLine()
-
getBeginColumn
int getBeginColumn()
-
getEndLine
int getEndLine()
-
getEndColumn
int getEndColumn()
-
getDataFlowNode
DataFlowNode getDataFlowNode()
-
setDataFlowNode
void setDataFlowNode(DataFlowNode dataFlowNode)
-
isFindBoundary
boolean isFindBoundary()
Returns true if this node is considered a boundary by traversal methods. Traversal methods such asgetFirstDescendantOfType(Class)
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.
-
getNthParent
Node getNthParent(int n)
Returns the n-th parent or null if there are less thann
ancestors.- Parameters:
n
- how many ancestors to iterate over.- Returns:
- the n-th parent or null.
- Throws:
java.lang.IllegalArgumentException
- ifn
is negative or zero.
-
getFirstParentOfType
<T> T getFirstParentOfType(java.lang.Class<T> parentType)
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
<T> java.util.List<T> getParentsOfType(java.lang.Class<T> parentType)
Traverses 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.
-
getFirstParentOfAnyType
<T> T getFirstParentOfAnyType(java.lang.Class<? extends T>... parentTypes)
Gets the first parent that's an instance of any of the given types.- Type Parameters:
T
- Most specific common type of the parameters- Parameters:
parentTypes
- Types to look for- Returns:
- The first parent with a matching type. Returns null if there is no such parent
-
findChildrenOfType
<T> java.util.List<T> findChildrenOfType(java.lang.Class<T> childType)
Traverses 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
<T> java.util.List<T> findDescendantsOfType(java.lang.Class<T> targetType)
Traverses 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
<T> void findDescendantsOfType(java.lang.Class<T> targetType, java.util.List<T> results, boolean crossFindBoundaries)
Traverses down the tree to find all the descendant instances of type descendantType.- Parameters:
targetType
- class which you want to find.results
- list to store the matching descendantscrossFindBoundaries
- iffalse
, recursion stops for nodes for whichisFindBoundary()
istrue
-
getFirstChildOfType
<T> T getFirstChildOfType(java.lang.Class<T> childType)
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
<T> T getFirstDescendantOfType(java.lang.Class<T> descendantType)
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
<T> boolean hasDescendantOfType(java.lang.Class<T> type)
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
java.util.List<? extends Node> findChildNodesWithXPath(java.lang.String xpathString) throws org.jaxen.JaxenException
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.
- Throws:
org.jaxen.JaxenException
- if the xpath is incorrect or fails altogether
-
hasDescendantMatchingXPath
boolean hasDescendantMatchingXPath(java.lang.String xpathString)
Checks whether at least one descendant matches the xpath expression.- Parameters:
xpathString
- the expression to check- Returns:
- true if there is a match
-
getAsDocument
org.w3c.dom.Document getAsDocument()
Get a DOM Document which contains Elements and Attributes representative of this Node and it's children. Essentially a DOM tree representation of the Node AST, thereby allowing tools which can operate upon DOM to also indirectly operate on the AST.
-
getUserData
java.lang.Object getUserData()
Get the user data associated with this node. By default there is no data, unless it has been set viasetUserData(Object)
.- Returns:
- The user data set on this node.
-
setUserData
void setUserData(java.lang.Object userData)
Set the user data associated with this node.PMD itself will never set user data onto a node. Nor should any Rule implementation, as the AST nodes are shared between concurrently executing Rules (i.e. it is not thread-safe).
This API is most useful for external applications looking to leverage PMD's robust support for AST structures, in which case application specific annotations on the AST nodes can be quite useful.
- Parameters:
userData
- The data to set on this node.
-
remove
void remove()
Remove the current node from its parent.
-
removeChildAtIndex
void removeChildAtIndex(int childIndex)
This method tells the node to remove the child node at the given index from the node's list of children, if any; if not, no changes are done.- Parameters:
childIndex
- The index of the child to be removed
-
getXPathNodeName
java.lang.String getXPathNodeName()
Gets the name of the node that is used to match it with XPath queries.- Returns:
- The XPath node name
-
getXPathAttributesIterator
java.util.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
-
-