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 that
N - 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.
  • Constructor Details

    • AbstractNode

      protected AbstractNode()
  • Method Details

    • 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 interface GenericNode<B extends AbstractNode<B,N>>
      Specified by:
      getParent in interface Node
      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 interface Node
      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 interface GenericNode<B extends AbstractNode<B,N>>
      Specified by:
      getChild in interface Node
    • getNumChildren

      public final int getNumChildren()
      Description copied from interface: Node
      Returns the number of children of this node.
      Specified by:
      getNumChildren in interface Node
    • 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 add
      index - 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 with addChild 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 child
      index - Index (must be 0 <= 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 interface Node
      Returns:
      The user data map of this node
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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 use Node.children(Class).
      Specified by:
      children in interface GenericNode<B extends AbstractNode<B,N>>
      Specified by:
      children in interface Node
      See Also:
    • 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 interface Node
      Type Parameters:
      R - Type of the child to find
      Parameters:
      rClass - Type of the child to find
      Returns:
      A child, or null