Class AbstractNode
- java.lang.Object
-
- net.sourceforge.pmd.lang.ast.AbstractNode
-
- All Implemented Interfaces:
Node
- Direct Known Subclasses:
AbstractJjtreeNode
public abstract class AbstractNode extends Object implements Node
Base class for all implementations of the Node interface.Please use the
Nodeinterface wherever possible and not this class, unless you're compelled to do so.Note that nearly all methods of the
Nodeinterface will have default implementations with PMD 7.0.0, so that it will not be necessary to extend this class directly.
-
-
Field Summary
Fields Modifier and Type Field Description protected intbeginColumnDeprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.protected intbeginLineDeprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.protected intchildIndexDeprecated.protected Node[]childrenDeprecated.protected intendColumnDeprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.protected intendLineDeprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.protected GenericTokenfirstTokenDeprecated.protected intidDeprecated.UsejjtGetId()if you are a jjtree node.protected GenericTokenlastTokenDeprecated.protected NodeparentDeprecated.UsegetParent()
-
Constructor Summary
Constructors Constructor Description AbstractNode(int id)AbstractNode(int id, int theBeginLine, int theEndLine, int theBeginColumn, int theEndColumn)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidappendElement(Node parentNode)Iterable<? extends Node>children()Returns an iterable enumerating the children of this node.List<Node>findChildNodesWithXPath(String xpathString)Returns all the nodes matching the xpath expression.<T> List<T>findChildrenOfType(Class<T> targetType)Traverses the children to find all the instances of type childType or one of its subclasses.<T> List<T>findDescendantsOfType(Class<? extends T> targetType)Traverses down the tree to find all the descendant instances of type descendantType without crossing find boundaries.<T> List<T>findDescendantsOfType(Class<T> targetType, boolean crossBoundaries)Traverses down the tree to find all the descendant instances of type descendantType.<T> voidfindDescendantsOfType(Class<T> targetType, List<T> results, boolean crossBoundaries)Deprecated.UsefindDescendantsOfType(Class, boolean)instead, which returns a result list.DocumentgetAsDocument()Deprecated.intgetBeginColumn()intgetBeginLine()NodegetChild(int index)Returns the child of this node at the given index.DataFlowNodegetDataFlowNode()intgetEndColumn()intgetEndLine()<T> TgetFirstChildOfType(Class<T> childType)Traverses the children to find the first instance of type childType.<T> TgetFirstDescendantOfType(Class<T> descendantType)Traverses down the tree to find the first descendant instance of type descendantType without crossing find boundaries.<T> TgetFirstParentOfAnyType(Class<? extends T>... parentTypes)Deprecated.<T> TgetFirstParentOfType(Class<T> parentType)Traverses up the tree to find the first parent instance of type parentType or one of its subclasses.StringgetImage()Returns a string token, usually filled-in by the parser, which describes some textual characteristic of this node.intgetIndexInParent()Returns the index of this node in its parent's children.NodegetNthParent(int n)Returns the n-th parent or null if there are less thannancestors.intgetNumChildren()Returns the number of children of this node.NodegetParent()Returns the parent of this node, or null if this is the root of the tree.<T> List<T>getParentsOfType(Class<T> parentType)Traverses up the tree to find all of the parent instances of type parentType or one of its subclasses.ObjectgetUserData()Deprecated.DataMap<DataMap.DataKey<?,?>>getUserMap()Returns a data map used to store additional information on this node.Iterator<Attribute>getXPathAttributesIterator()Returns an iterator enumerating all the attributes that are available from XPath for this node.StringgetXPathNodeName()Gets the name of the node that is used to match it with XPath queries.booleanhasDecendantOfAnyType(Class<?>... types)Deprecated.SeehasDescendantOfAnyType(Class[])for reasonsbooleanhasDescendantMatchingXPath(String xpathString)Checks whether at least one descendant matches the xpath expression.booleanhasDescendantOfAnyType(Class<?>... types)Deprecated.This is implemented inefficiently, with PMD 7 Node streams will provide a better alternative.<T> booleanhasDescendantOfType(Class<T> type)Finds if this node contains a descendant of the given type without crossing find boundaries.booleanhasImageEqualTo(String image)Returns true if this node's image is equal to the given string.booleanisFindBoundary()Returns true if this node is considered a boundary by traversal methods.booleanisSingleLine()Deprecated.This is never used and is trivial, will be removed from this class.voidjjtAddChild(Node child, int index)Deprecated.voidjjtClose()Deprecated.NodejjtGetChild(int index)Deprecated.intjjtGetChildIndex()Deprecated.GenericTokenjjtGetFirstToken()Deprecated.Not all nodes are based on tokens, and this is an implementation detailintjjtGetId()Deprecated.Will be made protected with 7.0.0.GenericTokenjjtGetLastToken()Deprecated.Not all nodes are based on tokens, and this is an implementation detailintjjtGetNumChildren()Deprecated.NodejjtGetParent()Deprecated.voidjjtOpen()Deprecated.voidjjtSetChildIndex(int index)Deprecated.voidjjtSetFirstToken(GenericToken token)Deprecated.This is JJTree-specific and will be removed from this superclass.voidjjtSetLastToken(GenericToken token)Deprecated.This is JJTree-specific and will be removed from this superclass.voidjjtSetParent(Node parent)Deprecated.voidremove()Deprecated.This is internal APIvoidremoveChildAtIndex(int childIndex)Deprecated.This is internal APIvoidsetDataFlowNode(DataFlowNode dataFlowNode)voidsetImage(String image)Deprecated.voidsetUserData(Object userData)Deprecated.voidtestingOnlySetBeginColumn(int i)Deprecated.This will be removed with 7.0.0voidtestingOnlySetBeginLine(int i)Deprecated.This will be removed with 7.0.0voidtestingOnlySetEndColumn(int i)Deprecated.This will be removed with 7.0.0voidtestingOnlySetEndLine(int i)Deprecated.This will be removed with 7.0.0StringtoString()Deprecated.The equivalence between toString and a node's name could be broken as soon as release 7.0.0.
-
-
-
Field Detail
-
parent
@Deprecated protected Node parent
Deprecated.UsegetParent()
-
children
@Deprecated protected Node[] children
Deprecated.
-
childIndex
@Deprecated protected int childIndex
Deprecated.
-
id
@Deprecated protected int id
Deprecated.UsejjtGetId()if you are a jjtree node.
-
beginLine
@Deprecated protected int beginLine
Deprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.
-
endLine
@Deprecated protected int endLine
Deprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.
-
beginColumn
@Deprecated protected int beginColumn
Deprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.
-
endColumn
@Deprecated protected int endColumn
Deprecated.This will be removed to delegate to the tokens for nodes that are backed by tokens.
-
firstToken
@Deprecated protected GenericToken firstToken
Deprecated.
-
lastToken
@Deprecated protected GenericToken lastToken
Deprecated.
-
-
Method Detail
-
getParent
public Node getParent()
Description copied from interface:NodeReturns the parent of this node, or null if this is the root of the tree.This method should be preferred to
Node.jjtGetParent().
-
getIndexInParent
public int getIndexInParent()
Description copied from interface:NodeReturns the index of this node in its parent's children. If this node is a root node, returns -1.This method replaces
Node.jjtGetChildIndex(), whose name was JJTree-specific.- Specified by:
getIndexInParentin interfaceNode- Returns:
- The index of this node in its parent's children
-
getChild
public Node getChild(int index)
Description copied from interface:NodeReturns the child of this node at the given index.
-
getNumChildren
public int getNumChildren()
Description copied from interface:NodeReturns the number of children of this node.- Specified by:
getNumChildrenin interfaceNode
-
isSingleLine
@Deprecated public boolean isSingleLine()
Deprecated.This is never used and is trivial, will be removed from this class.
-
jjtOpen
@Deprecated @InternalApi public void jjtOpen()
Deprecated.Description copied from interface:NodeThis method is called after the node has been made the current node. It indicates that child nodes can now be added to it.
-
jjtClose
@Deprecated @InternalApi public void jjtClose()
Deprecated.Description copied from interface:NodeThis method is called after all the child nodes have been added.
-
jjtSetParent
@Deprecated @InternalApi public void jjtSetParent(Node parent)
Deprecated.Description copied from interface:NodeSets the parent of this node.- Specified by:
jjtSetParentin interfaceNode- Parameters:
parent- The parent
-
jjtGetParent
@Deprecated public Node jjtGetParent()
Deprecated.Description copied from interface:NodeReturns the parent of this node.- Specified by:
jjtGetParentin interfaceNode- Returns:
- The parent of the node
-
jjtAddChild
@Deprecated @InternalApi public void jjtAddChild(Node child, int index)
Deprecated.Description copied from interface:NodeThis method tells the node to add its argument to the node's list of children.- Specified by:
jjtAddChildin interfaceNode- Parameters:
child- The child to addindex- The index to which the child will be added
-
jjtSetChildIndex
@Deprecated @InternalApi public void jjtSetChildIndex(int index)
Deprecated.Description copied from interface:NodeSets the index of this node from the perspective of its parent. This means: this.getParent().getChild(index) == this.- Specified by:
jjtSetChildIndexin interfaceNode- Parameters:
index- the child index
-
jjtGetChildIndex
@Deprecated public int jjtGetChildIndex()
Deprecated.Description copied from interface:NodeGets the index of this node in the children of its parent.- Specified by:
jjtGetChildIndexin interfaceNode- Returns:
- The index of the node
-
jjtGetChild
@Deprecated public Node jjtGetChild(int index)
Deprecated.Description copied from interface:NodeThis method returns a child node. The children are numbered from zero, left to right.- Specified by:
jjtGetChildin interfaceNode- Parameters:
index- the child index. Must be nonnegative and less thanNode.jjtGetNumChildren().
-
jjtGetNumChildren
@Deprecated public int jjtGetNumChildren()
Deprecated.Description copied from interface:NodeReturns the number of children the node has.- Specified by:
jjtGetNumChildrenin interfaceNode
-
jjtGetId
@Deprecated public int jjtGetId()
Deprecated.Will be made protected with 7.0.0.
-
getImage
public String getImage()
Description copied from interface:NodeReturns 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
@Deprecated public void setImage(String image)
Deprecated.
-
hasImageEqualTo
public boolean hasImageEqualTo(String image)
Description copied from interface:NodeReturns true if this node's image is equal to the given string.- Specified by:
hasImageEqualToin interfaceNode- Parameters:
image- The image to check
-
getBeginLine
public int getBeginLine()
- Specified by:
getBeginLinein interfaceNode
-
testingOnlySetBeginLine
@Deprecated @InternalApi public void testingOnlySetBeginLine(int i)
Deprecated.This will be removed with 7.0.0
-
getBeginColumn
public int getBeginColumn()
- Specified by:
getBeginColumnin interfaceNode
-
testingOnlySetBeginColumn
@Deprecated @InternalApi public void testingOnlySetBeginColumn(int i)
Deprecated.This will be removed with 7.0.0
-
getEndLine
public int getEndLine()
- Specified by:
getEndLinein interfaceNode
-
testingOnlySetEndLine
@Deprecated @InternalApi public void testingOnlySetEndLine(int i)
Deprecated.This will be removed with 7.0.0
-
getEndColumn
public int getEndColumn()
- Specified by:
getEndColumnin interfaceNode
-
testingOnlySetEndColumn
@Deprecated @InternalApi public void testingOnlySetEndColumn(int i)
Deprecated.This will be removed with 7.0.0
-
getDataFlowNode
public DataFlowNode getDataFlowNode()
- Specified by:
getDataFlowNodein interfaceNode
-
setDataFlowNode
public void setDataFlowNode(DataFlowNode dataFlowNode)
- Specified by:
setDataFlowNodein interfaceNode
-
getNthParent
public Node getNthParent(int n)
Description copied from interface:NodeReturns the n-th parent or null if there are less thannancestors.- Specified by:
getNthParentin interfaceNode- Parameters:
n- how many ancestors to iterate over.- Returns:
- the n-th parent or null.
-
getFirstParentOfType
public <T> T getFirstParentOfType(Class<T> parentType)
Description copied from interface:NodeTraverses up the tree to find the first parent instance of type parentType or one of its subclasses.- Specified by:
getFirstParentOfTypein interfaceNode- 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
public <T> List<T> getParentsOfType(Class<T> parentType)
Description copied from interface:NodeTraverses up the tree to find all of the parent instances of type parentType or one of its subclasses. The nodes are ordered deepest-first.- Specified by:
getParentsOfTypein interfaceNode- 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
@SafeVarargs @Deprecated public final <T> T getFirstParentOfAnyType(Class<? extends T>... parentTypes)
Deprecated.Description copied from interface:NodeGets the first parent that's an instance of any of the given types.- Specified by:
getFirstParentOfAnyTypein interfaceNode- 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
-
findDescendantsOfType
public <T> List<T> findDescendantsOfType(Class<? extends T> targetType)
Description copied from interface:NodeTraverses down the tree to find all the descendant instances of type descendantType without crossing find boundaries.- Specified by:
findDescendantsOfTypein interfaceNode- Parameters:
targetType- class which you want to find.- Returns:
- List of all children of type targetType. Returns an empty list if none found.
-
findDescendantsOfType
public <T> List<T> findDescendantsOfType(Class<T> targetType, boolean crossBoundaries)
Description copied from interface:NodeTraverses down the tree to find all the descendant instances of type descendantType.- Specified by:
findDescendantsOfTypein interfaceNode- Parameters:
targetType- class which you want to find.crossBoundaries- iffalse, recursion stops for nodes for whichNode.isFindBoundary()istrue- Returns:
- List of all matching descendants
-
findDescendantsOfType
@Deprecated public <T> void findDescendantsOfType(Class<T> targetType, List<T> results, boolean crossBoundaries)
Deprecated.UsefindDescendantsOfType(Class, boolean)instead, which returns a result list.Description copied from interface:NodeTraverses down the tree to find all the descendant instances of type descendantType.- Specified by:
findDescendantsOfTypein interfaceNode- Parameters:
targetType- class which you want to find.results- list to store the matching descendantscrossBoundaries- iffalse, recursion stops for nodes for whichNode.isFindBoundary()istrue
-
findChildrenOfType
public <T> List<T> findChildrenOfType(Class<T> targetType)
Description copied from interface:NodeTraverses the children to find all the instances of type childType or one of its subclasses.- Specified by:
findChildrenOfTypein interfaceNode- Parameters:
targetType- 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.
-
isFindBoundary
public boolean isFindBoundary()
Description copied from interface:NodeReturns true if this node is considered a boundary by traversal methods. Traversal methods such asNode.getFirstDescendantOfType(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.Note: This attribute is deprecated for XPath queries. It is not useful for XPath queries and will be removed with PMD 7.0.0.
- Specified by:
isFindBoundaryin interfaceNode
-
getAsDocument
@Deprecated public Document getAsDocument()
Deprecated.Description copied from interface:NodeGet 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.- Specified by:
getAsDocumentin interfaceNode
-
appendElement
protected void appendElement(Node parentNode)
-
getFirstDescendantOfType
public <T> T getFirstDescendantOfType(Class<T> descendantType)
Description copied from interface:NodeTraverses down the tree to find the first descendant instance of type descendantType without crossing find boundaries.- Specified by:
getFirstDescendantOfTypein interfaceNode- Parameters:
descendantType- class which you want to find.- Returns:
- Node of type descendantType. Returns
nullif none found.
-
getFirstChildOfType
public <T> T getFirstChildOfType(Class<T> childType)
Description copied from interface:NodeTraverses the children to find the first instance of type childType.- Specified by:
getFirstChildOfTypein interfaceNode- Parameters:
childType- class which you want to find.- Returns:
- Node of type childType. Returns
nullif none found. - See Also:
if traversal of the entire tree is needed.
-
hasDescendantOfType
public final <T> boolean hasDescendantOfType(Class<T> type)
Description copied from interface:NodeFinds if this node contains a descendant of the given type without crossing find boundaries.- Specified by:
hasDescendantOfTypein interfaceNode- Parameters:
type- the node type to search- Returns:
trueif there is at least one descendant of the given type
-
hasDecendantOfAnyType
@Deprecated public final boolean hasDecendantOfAnyType(Class<?>... types)
Deprecated.SeehasDescendantOfAnyType(Class[])for reasonsReturns true if this node has a descendant of any type among the provided types.- Parameters:
types- Types to test
-
hasDescendantOfAnyType
@Deprecated public final boolean hasDescendantOfAnyType(Class<?>... types)
Deprecated.This is implemented inefficiently, with PMD 7 Node streams will provide a better alternative. We cannot ensure binary compatibility because the methods on 7.0 expect at least one class type, by requiring one Class parameter before the varargs (Effective Java 2nd ed., Item 42).Returns true if this node has a descendant of any type among the provided types.- Parameters:
types- Types to test
-
findChildNodesWithXPath
public List<Node> findChildNodesWithXPath(String xpathString) throws org.jaxen.JaxenException
Description copied from interface:NodeReturns all the nodes matching the xpath expression.- Specified by:
findChildNodesWithXPathin interfaceNode- 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
public boolean hasDescendantMatchingXPath(String xpathString)
Description copied from interface:NodeChecks whether at least one descendant matches the xpath expression.- Specified by:
hasDescendantMatchingXPathin interfaceNode- Parameters:
xpathString- the expression to check- Returns:
- true if there is a match
-
getUserData
@Deprecated public Object getUserData()
Deprecated.Description copied from interface:NodeGet the user data associated with this node. By default there is no data, unless it has been set viaNode.setUserData(Object).- Specified by:
getUserDatain interfaceNode- Returns:
- The user data set on this node.
-
setUserData
@Deprecated public void setUserData(Object userData)
Deprecated.Description copied from interface:NodeSet 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.
- Specified by:
setUserDatain interfaceNode- Parameters:
userData- The data to set on this node.
-
getUserMap
public DataMap<DataMap.DataKey<?,?>> getUserMap()
Description copied from interface:NodeReturns a data map used to store additional information on this node. This replaces the legacyNode.getUserData()/Node.setUserData(Object).- Specified by:
getUserMapin interfaceNode- Returns:
- The user data map of this node
-
jjtGetFirstToken
@Deprecated public GenericToken jjtGetFirstToken()
Deprecated.Not all nodes are based on tokens, and this is an implementation detail
-
jjtSetFirstToken
@Deprecated public void jjtSetFirstToken(GenericToken token)
Deprecated.This is JJTree-specific and will be removed from this superclass.
-
jjtGetLastToken
@Deprecated public GenericToken jjtGetLastToken()
Deprecated.Not all nodes are based on tokens, and this is an implementation detail
-
jjtSetLastToken
@Deprecated public void jjtSetLastToken(GenericToken token)
Deprecated.This is JJTree-specific and will be removed from this superclass.
-
children
public Iterable<? extends Node> children()
Description copied from interface:NodeReturns an iterable enumerating the children of this node. Use it with a foreach loop:for (Node child : node.children()) { // process child }This method's return type will be changed to NodeStream in PMD 7, which is a more powerful kind of iterable. The change will be source compatible.
-
remove
@Deprecated @InternalApi public void remove()
Deprecated.This is internal APIDescription copied from interface:NodeRemove the current node from its parent.
-
removeChildAtIndex
@Deprecated @InternalApi public void removeChildAtIndex(int childIndex)
Deprecated.This is internal APIDescription copied from interface:NodeThis 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.- Specified by:
removeChildAtIndexin interfaceNode- Parameters:
childIndex- The index of the child to be removed
-
getXPathNodeName
public String getXPathNodeName()
Gets the name of the node that is used to match it with XPath queries.This default implementation adds compatibility with the previous way to get the xpath node name, which used
Object.toString().Please override it. It will be removed in version 7.0.0.
- Specified by:
getXPathNodeNamein interfaceNode- Returns:
- The XPath node name
-
toString
@Deprecated public String toString()
Deprecated.The equivalence between toString and a node's name could be broken as soon as release 7.0.0. Use getXPathNodeName for that purpose. The use for debugging purposes is not deprecated.
-
getXPathAttributesIterator
public Iterator<Attribute> getXPathAttributesIterator()
Description copied from interface:NodeReturns an iterator enumerating all the attributes that are available from XPath for this node.- Specified by:
getXPathAttributesIteratorin interfaceNode- Returns:
- An attribute iterator for this node
-
-