Interface NodeStream.DescendantNodeStream<T extends Node>

  • Type Parameters:
    T - Type of node this stream contains
    All Superinterfaces:
    Iterable<T>, NodeStream<T>
    Enclosing interface:
    NodeStream<T extends Node>

    public static interface NodeStream.DescendantNodeStream<T extends Node>
    extends NodeStream<T>
    A specialization of NodeStream that allows configuring tree traversal behaviour when traversing the descendants of a node. Such a stream is returned by methods such as Node.descendants(). When those methods are called on a stream containing more than one element (eg NodeStream.descendants()), the configuration applies to each individual traversal.

    By default, traversal is performed depth-first (prefix order). Eg

    
     A
     + B
       + C
       + D
     + E
       + F
     
    is traversed in the order A, B, C, D, E, F.

    By default, traversal also does not cross find boundaries.

    • Method Detail

      • crossFindBoundaries

        NodeStream.DescendantNodeStream<T> crossFindBoundaries​(boolean cross)
        Returns a node stream that will not stop the tree traversal when encountering a find boundary. Find boundaries are node that by default stop tree traversals, like class declarations. They are identified via Node.isFindBoundary().

        For example, supposing you have the AST node for the following method:

        
          void method() {
            String outer = "before";
        
            class Local {
              void localMethod() {
                String local = "local";
              }
            }
        
            String after = "after";
          }
         
        Then the stream method.descendants(ASTStringLiteral.class) will only yield the literals "before" and "after", because the traversal doesn't go below the local class.

        Note that traversal is stopped only for the subtree of the find boundary, but continues on the siblings. This is why "after" is yielded. This is also why NodeStream.takeWhile(Predicate) is not a substitute for this method: method.descendants(ASTStringLiteral.class).takeWhile(it -> !it.isFindBoundary) would yield only "before".

        This behaviour can be opted out of with this method. In the example, the stream method.descendants(ASTStringLiteral.class).crossFindBoundaries() will yield "before", "local" and "after" literals.

        Parameters:
        cross - If true, boundaries will be crossed.
        Returns:
        A new node stream