Package net.sourceforge.pmd.lang.ast
Class AbstractNode
- java.lang.Object
-
- net.sourceforge.pmd.lang.ast.AbstractNode
-
-
Field Summary
Fields Modifier and Type Field Description protected int
beginColumn
protected int
beginLine
protected int
childIndex
protected Node[]
children
protected int
endColumn
protected int
endLine
protected GenericToken
firstToken
protected int
id
protected GenericToken
lastToken
protected Node
parent
-
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 void
appendElement(org.w3c.dom.Node parentNode)
java.util.List<Node>
findChildNodesWithXPath(java.lang.String xpathString)
Returns all the nodes matching the xpath expression.<T> java.util.List<T>
findChildrenOfType(java.lang.Class<T> targetType)
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> java.util.List<T>
findDescendantsOfType(java.lang.Class<T> targetType, boolean crossBoundaries)
<T> void
findDescendantsOfType(java.lang.Class<T> targetType, java.util.List<T> results, boolean crossBoundaries)
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
hasDecendantOfAnyType(java.lang.Class<?>... types)
Deprecated.boolean
hasDescendantMatchingXPath(java.lang.String xpathString)
Checks whether at least one descendant matches the xpath expression.boolean
hasDescendantOfAnyType(java.lang.Class<?>... types)
Returns true if this node has a descendant of any type among the provided types.<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.boolean
isSingleLine()
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.GenericToken
jjtGetFirstToken()
int
jjtGetId()
GenericToken
jjtGetLastToken()
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
jjtSetFirstToken(GenericToken token)
void
jjtSetLastToken(GenericToken token)
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.void
testingOnlySetBeginColumn(int i)
void
testingOnlySetBeginLine(int i)
void
testingOnlySetEndColumn(int i)
void
testingOnlySetEndLine(int i)
java.lang.String
toString()
Deprecated.The equivalence between toString and a node's name could be broken as soon as release 7.0.0.
-
-
-
Field Detail
-
parent
protected Node parent
-
children
protected Node[] children
-
childIndex
protected int childIndex
-
id
protected int id
-
beginLine
protected int beginLine
-
endLine
protected int endLine
-
beginColumn
protected int beginColumn
-
endColumn
protected int endColumn
-
firstToken
protected GenericToken firstToken
-
lastToken
protected GenericToken lastToken
-
-
Method Detail
-
isSingleLine
public boolean isSingleLine()
-
jjtOpen
public void jjtOpen()
Description copied from interface:Node
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
public void jjtClose()
Description copied from interface:Node
This method is called after all the child nodes have been added.
-
jjtSetParent
public void jjtSetParent(Node parent)
Description copied from interface:Node
Sets the parent of this node.- Specified by:
jjtSetParent
in interfaceNode
- Parameters:
parent
- The parent
-
jjtGetParent
public Node jjtGetParent()
Description copied from interface:Node
Returns the parent of this node.- Specified by:
jjtGetParent
in interfaceNode
- Returns:
- The parent of the node
-
jjtAddChild
public void jjtAddChild(Node child, int index)
Description copied from interface:Node
This method tells the node to add its argument to the node's list of children.- Specified by:
jjtAddChild
in interfaceNode
- Parameters:
child
- The child to addindex
- The index to which the child will be added
-
jjtSetChildIndex
public void jjtSetChildIndex(int index)
Description copied from interface:Node
Sets the index of this node from the perspective of its parent. This means: this.jjtGetParent().jjtGetChild(index) == this.- Specified by:
jjtSetChildIndex
in interfaceNode
- Parameters:
index
- the child index
-
jjtGetChildIndex
public int jjtGetChildIndex()
Description copied from interface:Node
Gets the index of this node in the children of its parent.- Specified by:
jjtGetChildIndex
in interfaceNode
- Returns:
- The index of the node
-
jjtGetChild
public Node jjtGetChild(int index)
Description copied from interface:Node
This method returns a child node. The children are numbered from zero, left to right.- Specified by:
jjtGetChild
in interfaceNode
- Parameters:
index
- the child index. Must be nonnegative and less thanNode.jjtGetNumChildren()
.
-
jjtGetNumChildren
public int jjtGetNumChildren()
Description copied from interface:Node
Returns the number of children the node has.- Specified by:
jjtGetNumChildren
in interfaceNode
-
getImage
public java.lang.String getImage()
Description copied from interface:Node
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
public boolean hasImageEqualTo(java.lang.String image)
Description copied from interface:Node
Returns true if this node's image is equal to the given string.- Specified by:
hasImageEqualTo
in interfaceNode
- Parameters:
image
- The image to check
-
getBeginLine
public int getBeginLine()
- Specified by:
getBeginLine
in interfaceNode
-
testingOnlySetBeginLine
public void testingOnlySetBeginLine(int i)
-
getBeginColumn
public int getBeginColumn()
- Specified by:
getBeginColumn
in interfaceNode
-
testingOnlySetBeginColumn
public void testingOnlySetBeginColumn(int i)
-
getEndLine
public int getEndLine()
- Specified by:
getEndLine
in interfaceNode
-
testingOnlySetEndLine
public void testingOnlySetEndLine(int i)
-
getEndColumn
public int getEndColumn()
- Specified by:
getEndColumn
in interfaceNode
-
testingOnlySetEndColumn
public void testingOnlySetEndColumn(int i)
-
getDataFlowNode
public DataFlowNode getDataFlowNode()
- Specified by:
getDataFlowNode
in interfaceNode
-
setDataFlowNode
public void setDataFlowNode(DataFlowNode dataFlowNode)
- Specified by:
setDataFlowNode
in interfaceNode
-
getNthParent
public Node getNthParent(int n)
Description copied from interface:Node
Returns the n-th parent or null if there are less thann
ancestors.- Specified by:
getNthParent
in interfaceNode
- Parameters:
n
- how many ancestors to iterate over.- Returns:
- the n-th parent or null.
-
getFirstParentOfType
public <T> T getFirstParentOfType(java.lang.Class<T> parentType)
Description copied from interface:Node
Traverses up the tree to find the first parent instance of type parentType or one of its subclasses.- Specified by:
getFirstParentOfType
in 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> java.util.List<T> getParentsOfType(java.lang.Class<T> parentType)
Description copied from interface:Node
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.- Specified by:
getParentsOfType
in 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 public final <T> T getFirstParentOfAnyType(java.lang.Class<? extends T>... parentTypes)
Description copied from interface:Node
Gets the first parent that's an instance of any of the given types.- Specified by:
getFirstParentOfAnyType
in 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> java.util.List<T> findDescendantsOfType(java.lang.Class<T> targetType)
Description copied from interface:Node
Traverses down the tree to find all the descendant instances of type descendantType without crossing find boundaries.- Specified by:
findDescendantsOfType
in 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> java.util.List<T> findDescendantsOfType(java.lang.Class<T> targetType, boolean crossBoundaries)
-
findDescendantsOfType
public <T> void findDescendantsOfType(java.lang.Class<T> targetType, java.util.List<T> results, boolean crossBoundaries)
Description copied from interface:Node
Traverses down the tree to find all the descendant instances of type descendantType.- Specified by:
findDescendantsOfType
in 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> java.util.List<T> findChildrenOfType(java.lang.Class<T> targetType)
Description copied from interface:Node
Traverses the children to find all the instances of type childType or one of its subclasses.- Specified by:
findChildrenOfType
in 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:Node
Returns 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.- Specified by:
isFindBoundary
in interfaceNode
-
getAsDocument
public org.w3c.dom.Document getAsDocument()
Description copied from interface:Node
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.- Specified by:
getAsDocument
in interfaceNode
-
appendElement
protected void appendElement(org.w3c.dom.Node parentNode)
-
getFirstDescendantOfType
public <T> T getFirstDescendantOfType(java.lang.Class<T> descendantType)
Description copied from interface:Node
Traverses down the tree to find the first descendant instance of type descendantType without crossing find boundaries.- Specified by:
getFirstDescendantOfType
in interfaceNode
- Parameters:
descendantType
- class which you want to find.- Returns:
- Node of type descendantType. Returns
null
if none found.
-
getFirstChildOfType
public <T> T getFirstChildOfType(java.lang.Class<T> childType)
Description copied from interface:Node
Traverses the children to find the first instance of type childType.- Specified by:
getFirstChildOfType
in interfaceNode
- 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.
-
hasDescendantOfType
public final <T> boolean hasDescendantOfType(java.lang.Class<T> type)
Description copied from interface:Node
Finds if this node contains a descendant of the given type without crossing find boundaries.- Specified by:
hasDescendantOfType
in interfaceNode
- Parameters:
type
- the node type to search- Returns:
true
if there is at least one descendant of the given type
-
hasDecendantOfAnyType
@Deprecated public final boolean hasDecendantOfAnyType(java.lang.Class<?>... types)
Deprecated.Returns true if this node has a descendant of any type among the provided types.- Parameters:
types
- Types to test
-
hasDescendantOfAnyType
public final boolean hasDescendantOfAnyType(java.lang.Class<?>... types)
Returns true if this node has a descendant of any type among the provided types.- Parameters:
types
- Types to test
-
findChildNodesWithXPath
public java.util.List<Node> findChildNodesWithXPath(java.lang.String xpathString) throws org.jaxen.JaxenException
Description copied from interface:Node
Returns all the nodes matching the xpath expression.- Specified by:
findChildNodesWithXPath
in 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(java.lang.String xpathString)
Description copied from interface:Node
Checks whether at least one descendant matches the xpath expression.- Specified by:
hasDescendantMatchingXPath
in interfaceNode
- Parameters:
xpathString
- the expression to check- Returns:
- true if there is a match
-
getUserData
public java.lang.Object getUserData()
Description copied from interface:Node
Get the user data associated with this node. By default there is no data, unless it has been set viaNode.setUserData(Object)
.- Specified by:
getUserData
in interfaceNode
- Returns:
- The user data set on this node.
-
setUserData
public void setUserData(java.lang.Object userData)
Description copied from interface:Node
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.
- Specified by:
setUserData
in interfaceNode
- Parameters:
userData
- The data to set on this node.
-
jjtGetFirstToken
public GenericToken jjtGetFirstToken()
-
jjtSetFirstToken
public void jjtSetFirstToken(GenericToken token)
-
jjtGetLastToken
public GenericToken jjtGetLastToken()
-
jjtSetLastToken
public void jjtSetLastToken(GenericToken token)
-
remove
public void remove()
Description copied from interface:Node
Remove the current node from its parent.
-
removeChildAtIndex
public void removeChildAtIndex(int childIndex)
Description copied from interface:Node
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.- Specified by:
removeChildAtIndex
in interfaceNode
- Parameters:
childIndex
- The index of the child to be removed
-
getXPathNodeName
public java.lang.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 may be removed in a future major version.
- Specified by:
getXPathNodeName
in interfaceNode
- Returns:
- The XPath node name
-
toString
@Deprecated public java.lang.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.- Overrides:
toString
in classjava.lang.Object
-
getXPathAttributesIterator
public java.util.Iterator<Attribute> getXPathAttributesIterator()
Description copied from interface:Node
Returns an iterator enumerating all the attributes that are available from XPath for this node.- Specified by:
getXPathAttributesIterator
in interfaceNode
- Returns:
- An attribute iterator for this node
-
-