diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java
index efd5978dff..038a44e6b0 100644
--- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java
+++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java
@@ -19,6 +19,7 @@ import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.internal.util.IteratorUtil;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.NodeStream;
+import net.sourceforge.pmd.lang.ast.NodeStream.DescendantNodeStream;
/**
* Optimised node stream implementation for a single element. Streams
@@ -32,11 +33,12 @@ import net.sourceforge.pmd.lang.ast.NodeStream;
*
This ensures that short pipelines like {@code node.descendants().first()}
* are as efficient as the pre 7.0.0 methods.
*/
-final class SingletonNodeStream extends IteratorBasedNStream {
+final class SingletonNodeStream extends IteratorBasedNStream implements DescendantNodeStream {
private final T node;
SingletonNodeStream(@NonNull T node) {
+ assert node != null : "null node!";
this.node = node;
}
@@ -207,4 +209,9 @@ final class SingletonNodeStream extends IteratorBasedNStream
public NodeStream precedingSiblings() {
return StreamImpl.precedingSiblings(node);
}
+
+ @Override
+ public DescendantNodeStream crossFindBoundaries(boolean cross) {
+ return this; // doesn't mean anything
+ }
}
diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java
index edac7e1b1e..c70239ed17 100644
--- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java
+++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java
@@ -38,7 +38,7 @@ public final class StreamImpl {
// utility class
}
- public static NodeStream singleton(@NonNull T node) {
+ public static DescendantNodeStream singleton(@NonNull T node) {
return new SingletonNodeStream<>(node);
}
@@ -88,7 +88,7 @@ public final class StreamImpl {
}
public static DescendantNodeStream descendantsOrSelf(@NonNull Node node) {
- return node.getNumChildren() == 0 ? empty() : new DescendantOrSelfStream(node, TreeWalker.DEFAULT);
+ return node.getNumChildren() == 0 ? singleton(node) : new DescendantOrSelfStream(node, TreeWalker.DEFAULT);
}
public static NodeStream followingSiblings(@NonNull Node node) {
diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java
index 9c4b738912..0bc4b03a1c 100644
--- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java
+++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamTest.java
@@ -124,6 +124,7 @@ public class NodeStreamTest {
public void testDescendantOrSelfStream() {
assertThat(pathsOf(tree1.descendantsOrSelf()), contains("", "0", "00", "01", "010", "011", "0110", "012", "013", "1"));
assertThat(pathsOf(NodeStream.of(tree1).descendantsOrSelf()), contains("", "0", "00", "01", "010", "011", "0110", "012", "013", "1"));
+ assertThat(pathsOf(followPath(tree1, "0110").descendantsOrSelf()), contains("0110")); // with a leaf node
}
@Test