Class AbstractNode<B extends AbstractNode<B,N>,N extends Node & GenericNode<N>>
java.lang.Object
net.sourceforge.pmd.lang.ast.impl.AbstractNode<B,N>
- Type Parameters:
B- Self type (eg AbstractJavaNode in the java module), this must ultimately implement<N>, though the java type system does not allow us to express thatN- Public interface for nodes of this language (eg JavaNode in the java module).
- All Implemented Interfaces:
GenericNode<N>,Node,Reportable
- Direct Known Subclasses:
AbstractJjtreeNode,PlainTextLanguage.PlainTextFile
public abstract class AbstractNode<B extends AbstractNode<B,N>,N extends Node & GenericNode<N>>
extends Object
implements GenericNode<N>
Base class for implementations of the Node interface whose children
are stored in an array. This class provides the basic utilities to
link children and parent. It's used by most most nodes, but currently
not the antlr nodes, so downcasting
Node to this class may fail
and is very bad practice.-
Field Summary
Fields inherited from interface net.sourceforge.pmd.lang.ast.Node
COORDS_COMPARATOR -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidSet the child at the given index to the given node.final NodeStream<N>children()Returns a node stream containing all the children of this node.final <R extends Node>
@Nullable RfirstChild(Class<? extends R> rClass) Returns the first child of this node that has the given type.final NgetChild(int index) Returns the child of this node at the given index.final intReturns the index of this node in its parent's children.final intReturns the number of children of this node.final NReturns the parent of this node, or null if this is the root of the tree.DataMap<DataMap.DataKey<?,?>> Returns a data map used to store additional information on this node.protected voidinsertChild(B child, int index) Insert a child at the given index, shifting all the following children to the right.protected voidremove()protected voidremoveChildAtIndex(int childIndex) protected voidSet the child at the given index.protected voidtoString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.sourceforge.pmd.lang.ast.impl.GenericNode
ancestors, ancestorsOrSelf, asStream, descendants, descendantsOrSelf, getFirstChild, getLastChild, getNextSibling, getPreviousSiblingMethods inherited from interface net.sourceforge.pmd.lang.ast.Node
acceptVisitor, ancestors, children, compareLocation, descendants, getAstInfo, getBeginColumn, getBeginLine, getEndColumn, getEndLine, getImage, getLanguageVersion, getReportLocation, getRoot, getTextDocument, getTextRegion, getXPathAttributesIterator, getXPathNodeName, hasImageEqualTo, isFindBoundary
-
Constructor Details
-
AbstractNode
protected AbstractNode()
-
-
Method Details
-
getParent
Description copied from interface:NodeReturns the parent of this node, or null if this is the root of the tree.- Specified by:
getParentin interfaceGenericNode<B extends AbstractNode<B,N>> - Specified by:
getParentin interfaceNode- Returns:
- The parent of this node
-
getIndexInParent
public final 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.- Specified by:
getIndexInParentin interfaceNode- Returns:
- The index of this node in its parent's children
-
getChild
Description copied from interface:NodeReturns the child of this node at the given index.- Specified by:
getChildin interfaceGenericNode<B extends AbstractNode<B,N>> - Specified by:
getChildin interfaceNode
-
getNumChildren
public final int getNumChildren()Description copied from interface:NodeReturns the number of children of this node.- Specified by:
getNumChildrenin interfaceNode
-
setParent
-
addChild
Set the child at the given index to the given node. This resizes the children array to be able to contain the given index. Implementations must take care that this does not leave any "holes" in the array. This method throws if there is already a child at the given index.Note that it is more efficient to add children in reverse (from right to left), because the array is resized only the first time.
This method also calls
setParent(AbstractNode).- Parameters:
child- The child to addindex- The index to which the child will be added
-
setChild
Set the child at the given index. The difference withaddChildis that the index must exist, while addChild may resizes the array. -
insertChild
Insert a child at the given index, shifting all the following children to the right.- Parameters:
child- New childindex- Index (must be0 <= index <= getNumChildren()), i.e. you cannot insert a node beyond the end, because that would leave holes in the array
-
remove
protected void remove() -
removeChildAtIndex
protected void removeChildAtIndex(int childIndex) -
getUserMap
Description copied from interface:NodeReturns a data map used to store additional information on this node.- Specified by:
getUserMapin interfaceNode- Returns:
- The user data map of this node
-
toString
-
children
Description copied from interface:NodeReturns a node stream containing all the children of this node. This method does not provide much type safety, you'll probably want to useNode.children(Class).- Specified by:
childrenin interfaceGenericNode<B extends AbstractNode<B,N>> - Specified by:
childrenin interfaceNode- See Also:
-
firstChild
Description copied from interface:NodeReturns 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.- Specified by:
firstChildin interfaceNode- Type Parameters:
R- Type of the child to find- Parameters:
rClass- Type of the child to find- Returns:
- A child, or null
-