Class ASTList<N extends JavaNode>

java.lang.Object
net.sourceforge.pmd.lang.ast.impl.AbstractNode<B,N>
net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
net.sourceforge.pmd.lang.java.ast.ASTList<N>
Type Parameters:
N - Type of node contained within this list node
All Implemented Interfaces:
Iterable<N>, GenericNode<JavaNode>, JjtreeNode<JavaNode>, Node, TextAvailableNode, JavaNode, Reportable
Direct Known Subclasses:
ASTAnnotationMemberList, ASTArgumentList, ASTArrayDimensions, ASTBlock, ASTExtendsList, ASTFormalParameters, ASTImplementsList, ASTLambdaParameterList, ASTPatternList, ASTPermitsList, ASTRecordComponentList, ASTResourceList, ASTStatementExpressionList, ASTThrowsList, ASTTypeArguments, ASTTypeBody, ASTTypeParameters

public abstract class ASTList<N extends JavaNode> extends AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode> implements Iterable<N>
Common supertype for nodes that act as a kind of list of other nodes. This is mainly provided as a way to share API and not a structural distinction in the AST.

This node can be converted to a list with toList(). Often these nodes are optional in their parent, and so might be null. The method orEmpty helps in such cases. For example


 // This will throw NullPointerException if the class is not generic.
 for (ASTTypeParameter tparam : classDecl.getTypeParameters()) {

 }
 
Instead of explicitly checking for null, which is annoying, use the following idiom:

 for (ASTTypeParameter tparam : ASTList.orEmpty(classDecl.getTypeParameters())) {

 }
 

Note that though it is usually the case that the node lists all its children, there is no guarantee about that. For instance, ASTFormalParameters excludes the receiver parameter.

  • Field Details

  • Method Details

    • size

      public int size()
      Returns the number of nodes in this list. This must be the number of nodes yielded by the iterator().
    • isEmpty

      public boolean isEmpty()
    • toList

      public List<N> toList()
      Returns a list containing the element of this node.
    • toStream

      public NodeStream<N> toStream()
      Returns a node stream containing the same element this node contains.
    • iterator

      public Iterator<N> iterator()
      Specified by:
      iterator in interface Iterable<N extends JavaNode>
    • get

      public N get(int i)
      Throws:
      IndexOutOfBoundsException - if not in range
    • orEmpty

      public static <N extends JavaNode> @NonNull List<N> orEmpty(@Nullable ASTList<N> list)
      Returns an empty list if the parameter is null, otherwise returns its toList().
      Type Parameters:
      N - Type of elements
      Parameters:
      list - List node
      Returns:
      A non-null list
    • orEmptyStream

      public static <N extends JavaNode> @NonNull NodeStream<N> orEmptyStream(@Nullable ASTList<N> list)
    • sizeOrZero

      public static int sizeOrZero(@Nullable ASTList<?> list)
    • singleOrNull

      public static <N extends JavaNode> @Nullable N singleOrNull(@Nullable ASTList<N> list)
      Returns the element if there is exactly one, otherwise returns null.
      Type Parameters:
      N - Type of elements
      Parameters:
      list - List node
      Returns:
      An element, or null.
    • jjtClose

      public void jjtClose()
      Overrides:
      jjtClose in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • acceptVisitor

      public final <P, R> R acceptVisitor(AstVisitor<? super P,? extends R> visitor, P data)
      Specified by:
      acceptVisitor in interface Node
    • acceptVisitor

      protected abstract <P, R> R acceptVisitor(JavaVisitor<? super P,? extends R> visitor, P data)
    • addChild

      protected void addChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
      Overrides:
      addChild in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • insertChild

      protected void insertChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
      Overrides:
      insertChild in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • removeChildAtIndex

      protected void removeChildAtIndex(int childIndex)
      Overrides:
      removeChildAtIndex in class AbstractNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • setImage

      protected void setImage(String image)
      Overrides:
      setImage in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • setFirstToken

      protected void setFirstToken(JavaccToken token)
      Overrides:
      setFirstToken in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • setLastToken

      protected void setLastToken(JavaccToken token)
      Overrides:
      setLastToken in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • enlargeLeft

      protected void enlargeLeft(JavaccToken child)
      Overrides:
      enlargeLeft in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • setChild

      protected void setChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
      Overrides:
      setChild in class AbstractNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • getSymbolTable

      public @NonNull JSymbolTable getSymbolTable()
      Description copied from interface: JavaNode
      Returns the symbol table for the program point represented by this node.
      Specified by:
      getSymbolTable in interface JavaNode
    • getTypeSystem

      public TypeSystem getTypeSystem()
      Description copied from interface: JavaNode
      Returns the type system with which this node was created. This is the object responsible for representing types in the compilation unit.
      Specified by:
      getTypeSystem in interface JavaNode
    • getRoot

      public final @NonNull ASTCompilationUnit getRoot()
      Specified by:
      getRoot in interface JavaNode
      Specified by:
      getRoot in interface Node
    • getXPathNodeName

      public final String getXPathNodeName()
      Specified by:
      getXPathNodeName in interface Node