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 downcastingNode
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 Modifier Constructor Description protected
AbstractNode()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addChild(B child, int index)
Set the child at the given index to the given node.NodeStream<N>
children()
Returns a node stream containing all the children of this node.<R extends Node>
@Nullable RfirstChild(Class<? extends R> rClass)
Returns the first child of this node that has the given type.N
getChild(int index)
Returns the child of this node at the given index.int
getIndexInParent()
Returns the index of this node in its parent's children.int
getNumChildren()
Returns the number of children of this node.N
getParent()
Returns the parent of this node, or null if this is the root of the tree.DataMap<DataMap.DataKey<?,?>>
getUserMap()
Returns a data map used to store additional information on this node.protected void
insertChild(B child, int index)
Insert a child at the given index, shifting all the following children to the right.protected void
remove()
protected void
removeChildAtIndex(int childIndex)
protected void
setChild(B child, int index)
Set the child at the given index.protected void
setParent(B parent)
String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.impl.GenericNode
ancestors, ancestorsOrSelf, asStream, descendants, descendantsOrSelf, getFirstChild, getLastChild, getNextSibling, getPreviousSibling
-
Methods 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
-
-
-
-
Method Detail
-
getParent
public final N getParent()
Description copied from interface:Node
Returns the parent of this node, or null if this is the root of the tree.- Specified by:
getParent
in interfaceGenericNode<B extends AbstractNode<B,N>>
- Specified by:
getParent
in interfaceNode
- Returns:
- The parent of this node
-
getIndexInParent
public final int getIndexInParent()
Description copied from interface:Node
Returns the index of this node in its parent's children. If this node is a root node, returns -1.- Specified by:
getIndexInParent
in interfaceNode
- Returns:
- The index of this node in its parent's children
-
getChild
public final N getChild(int index)
Description copied from interface:Node
Returns the child of this node at the given index.- Specified by:
getChild
in interfaceGenericNode<B extends AbstractNode<B,N>>
- Specified by:
getChild
in interfaceNode
-
getNumChildren
public final int getNumChildren()
Description copied from interface:Node
Returns the number of children of this node.- Specified by:
getNumChildren
in interfaceNode
-
setParent
protected void setParent(B parent)
-
addChild
protected void addChild(B child, int index)
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
protected void setChild(B child, int index)
Set the child at the given index. The difference withaddChild
is that the index must exist, while addChild may resizes the array.
-
insertChild
protected void insertChild(B child, int index)
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
public DataMap<DataMap.DataKey<?,?>> getUserMap()
Description copied from interface:Node
Returns a data map used to store additional information on this node.- Specified by:
getUserMap
in interfaceNode
- Returns:
- The user data map of this node
-
children
public final NodeStream<N> children()
Description copied from interface:Node
Returns 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:
children
in interfaceGenericNode<B extends AbstractNode<B,N>>
- Specified by:
children
in interfaceNode
- See Also:
NodeStream.children(Class)
-
firstChild
public final <R extends Node> @Nullable R firstChild(Class<? extends R> rClass)
Description copied from interface:Node
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.- Specified by:
firstChild
in interfaceNode
- Type Parameters:
R
- Type of the child to find- Parameters:
rClass
- Type of the child to find- Returns:
- A child, or null
-
-