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>
public static interface NodeStream.DescendantNodeStream<T extends Node> extends NodeStream<T>
A specialization ofNodeStream
that allows configuring tree traversal behaviour when traversing the descendants of a node. Such a stream is returned by methods such asNode.descendants()
. When those methods are called on a stream containing more than one element (egNodeStream.descendants()
), the configuration applies to each individual traversal.By default, traversal is performed depth-first (prefix order). Eg
is traversed in the orderA + B + C + D + E + F
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
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default NodeStream.DescendantNodeStream<T>
crossFindBoundaries()
An alias forcrossFindBoundaries(true)
.NodeStream.DescendantNodeStream<T>
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, spliterator
-
Methods 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 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 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
default NodeStream.DescendantNodeStream<T> crossFindBoundaries()
An alias forcrossFindBoundaries(true)
.- Returns:
- A new node stream
-
-