From ab0aaadd64f243bbe155377557dc72f209dd2dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 7 Aug 2020 02:58:27 +0200 Subject: [PATCH] Fix descendantOrSelf on leaf With a leaf node it used to return empty --- .../pmd/lang/ast/internal/SingletonNodeStream.java | 9 ++++++++- .../sourceforge/pmd/lang/ast/internal/StreamImpl.java | 4 ++-- .../pmd/lang/ast/internal/NodeStreamTest.java | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) 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