Package net.sourceforge.pmd.lang.ast
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>
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.sourceforge.pmd.lang.ast.NodeStream
NodeStream.DescendantNodeStream<T extends Node> -
Method Summary
Modifier and TypeMethodDescriptiondefault NodeStream.DescendantNodeStream<T> An alias forcrossFindBoundaries(true).crossFindBoundaries(boolean cross) Returns a node stream that will not stop the tree traversal when encountering a find boundary.Methods inherited from interface java.lang.Iterable
iterator, spliteratorMethods inherited from interface net.sourceforge.pmd.lang.ast.NodeStream
all, ancestors, ancestors, ancestorsOrSelf, any, append, cached, children, children, collect, count, descendants, descendants, descendantsOrSelf, distinct, drop, dropLast, filter, filterIs, filterMatching, filterNot, filterNotMatching, first, first, first, firstChild, firstNonNull, firstOpt, firstOrThrow, flatMap, followingSiblings, forEach, get, isEmpty, last, last, map, none, nonEmpty, parents, peek, precedingSiblings, prepend, reduce, sumBy, sumByInt, take, takeWhile, toList, toList, toStream
-
Method Details
-
crossFindBoundaries
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 viaNode.isFindBoundary().For example, supposing you have the AST node for the following method:
Then the streamvoid method() { String outer = "before"; class Local { void localMethod() { String local = "local"; } } String after = "after"; }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 whyNodeStream.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
-
crossFindBoundaries
An alias forcrossFindBoundaries(true).- Returns:
- A new node stream
-