From 434881edac80cd0763ec752fbca31ecfd2b30662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Wed, 22 Apr 2020 00:47:44 +0200 Subject: [PATCH] Fix tests --- .../pmd/lang/ast/impl/AbstractNode.java | 4 +-- .../impl/AbstractNodeWithTextCoordinates.java | 3 ++ .../pmd/lang/ast/impl/GenericNode.java | 6 ++++ .../lang/ast/impl/javacc/JjtreeBuilder.java | 4 +-- .../ast/internal/IteratorBasedNStream.java | 8 +++--- .../pmd/lang/ast/internal/StreamImpl.java | 2 +- .../lang/ast/xpath/AttributeAxisIterator.java | 5 ++-- .../pmd/lang/ast/xpath/NoAttribute.java | 2 +- .../net/sourceforge/pmd/AbstractRuleTest.java | 4 +-- .../pmd/RuleViolationComparatorTest.java | 22 +++++++-------- .../sourceforge/pmd/RuleViolationTest.java | 6 ++-- .../pmd/jaxen/MatchesFunctionTest.java | 4 +-- .../ast/internal/NodeStreamBlanketTest.java | 6 ++-- .../pmd/lang/ast/internal/NodeStreamTest.java | 14 ++++++---- .../ast/xpath/AttributeAxisIteratorTest.java | 19 ++++--------- .../pmd/lang/ast/xpath/NoAttributeTest.java | 28 ++++--------------- .../pmd/lang/rule/XPathRuleTest.java | 2 +- .../rule/xpath/saxon/ElementNodeTest.java | 2 +- .../ast/AbstractEcmascriptNode.java | 5 +++- .../ast/EcmascriptParserVisitor.java | 2 +- .../ecmascript/ast/EcmascriptTreeBuilder.java | 8 ++---- .../pmd/lang/ast/test/NodeExtensions.kt | 7 ----- .../pmd/lang/scala/ast/AbstractScalaNode.java | 4 +++ .../pmd/lang/scala/ast/ScalaTreeBuilder.java | 17 +++++------ .../pmd/test/lang/DummyLanguageModule.java | 9 ++---- .../pmd/test/lang/ast/DummyNode.java | 6 +++- .../pmd/testframework/RuleTstTest.java | 6 ++-- .../pmd/lang/vm/ast/AbstractVmNode.java | 10 ------- 28 files changed, 91 insertions(+), 124 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNode.java index 96d6fb9368..f1170c7b5c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNode.java @@ -44,8 +44,8 @@ public abstract class AbstractNode> implements GenericN // @Deprecated? private String image; - public AbstractNode() { - + protected AbstractNode() { + // only for subclassing } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNodeWithTextCoordinates.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNodeWithTextCoordinates.java index 81b1f19e28..6f4154a45e 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNodeWithTextCoordinates.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AbstractNodeWithTextCoordinates.java @@ -16,6 +16,7 @@ public abstract class AbstractNodeWithTextCoordinates> protected int endColumn = -1; protected AbstractNodeWithTextCoordinates() { + // only for subclassing } @Override @@ -39,6 +40,8 @@ public abstract class AbstractNodeWithTextCoordinates> } protected void setCoords(int bline, int bcol, int eline, int ecol) { + assert bline >= 1 && bcol >= 1 && eline >= 1 && ecol >= 1 : "coordinates are 1-based"; + assert bline <= eline && (bline != eline || bcol <= ecol) : "coordinates must be ordered"; beginLine = bline; beginColumn = bcol; endLine = eline; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/GenericNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/GenericNode.java index 408222ea91..714529896b 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/GenericNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/GenericNode.java @@ -1,6 +1,7 @@ /* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ + package net.sourceforge.pmd.lang.ast.impl; @@ -39,6 +40,11 @@ public interface GenericNode> extends Node { return (DescendantNodeStream) Node.super.descendants(); } + @Override + default DescendantNodeStream descendantsOrSelf() { + return (DescendantNodeStream) Node.super.descendantsOrSelf(); + } + @Override default NodeStream ancestorsOrSelf() { return (NodeStream) Node.super.ancestorsOrSelf(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java index 072a3a6834..5273ca0f3c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java @@ -7,15 +7,13 @@ package net.sourceforge.pmd.lang.ast.impl.javacc; import java.util.ArrayList; import java.util.List; -import net.sourceforge.pmd.lang.ast.Node; - /** * Shared implementation of the tree builder generated by JJTree. * * @param Internal base class * @param

Public interface (this is just nee */ -public final class JjtreeBuilder, P extends Node> { +public final class JjtreeBuilder, P extends JjtreeNode

> { private final List nodes = new ArrayList<>(); private final List marks = new ArrayList<>(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java index b8e7c20339..afbf7d3a35 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java @@ -91,7 +91,7 @@ abstract class IteratorBasedNStream implements NodeStream { @NonNull - protected DescendantNodeStream flatMapDescendants(Function> mapper) { + protected DescendantNodeStream flatMapDescendants(Function> mapper) { return new DescendantMapping<>(this, mapper); } @@ -254,18 +254,18 @@ abstract class IteratorBasedNStream implements NodeStream { private static class DescendantMapping extends IteratorBasedNStream implements DescendantNodeStream { - private final Function> fun; + private final Function> fun; private final TreeWalker walker; private final IteratorBasedNStream upstream; - private DescendantMapping(IteratorBasedNStream upstream, Function> fun, TreeWalker walker) { + private DescendantMapping(IteratorBasedNStream upstream, Function> fun, TreeWalker walker) { this.fun = fun; this.walker = walker; this.upstream = upstream; } - DescendantMapping(IteratorBasedNStream upstream, Function> fun) { + DescendantMapping(IteratorBasedNStream upstream, Function> fun) { this(upstream, fun, TreeWalker.DEFAULT); } 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 62addf9e67..535653f401 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 @@ -181,7 +181,7 @@ public final class StreamImpl { } @Override - protected @NonNull DescendantNodeStream flatMapDescendants(Function> mapper) { + protected @NonNull DescendantNodeStream flatMapDescendants(Function> mapper) { return StreamImpl.empty(); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java index 1797a27311..b659590147 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java @@ -17,8 +17,9 @@ import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; import net.sourceforge.pmd.annotation.InternalApi; -import net.sourceforge.pmd.lang.ast.impl.AbstractNode; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.impl.AbstractNode; +import net.sourceforge.pmd.lang.ast.impl.AbstractNodeWithTextCoordinates; import net.sourceforge.pmd.lang.ast.xpath.NoAttribute.NoAttrScope; @@ -110,7 +111,7 @@ public class AttributeAxisIterator implements Iterator { Class declaration = method.getDeclaringClass(); if (method.isAnnotationPresent(NoAttribute.class)) { return true; - } else if (declaration == Node.class || declaration == AbstractNode.class) { + } else if (declaration == Node.class || declaration == AbstractNode.class || declaration == AbstractNodeWithTextCoordinates.class) { // attributes from Node and AbstractNode are never suppressed // we don't know what might go wrong if we do suppress them return false; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NoAttribute.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NoAttribute.java index 7e78c9706e..a359b844f0 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NoAttribute.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NoAttribute.java @@ -9,8 +9,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import net.sourceforge.pmd.lang.ast.impl.AbstractNode; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.impl.AbstractNode; /** * Filters out some methods from the XPath attributes of a node. diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/AbstractRuleTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/AbstractRuleTest.java index ef608b7709..276c8d306c 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/AbstractRuleTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/AbstractRuleTest.java @@ -111,7 +111,7 @@ public class AbstractRuleTest { ctx.setReport(new Report()); ctx.setSourceCodeFile(new File("filename")); DummyNode s = new DummyRoot(); - s.setCoords(5, 1, 6, 0); + s.setCoords(5, 1, 6, 1); s.setImage("TestImage"); r.addViolation(ctx, s); RuleViolation rv = ctx.getReport().getViolationTree().iterator().next(); @@ -126,7 +126,7 @@ public class AbstractRuleTest { m.put(5, ""); ctx.setSourceCodeFile(new File("filename")); DummyRoot n = new DummyRoot(m); - n.setCoords(5, 1, 6, 0); + n.setCoords(5, 1, 6, 1); DefaultRuleViolationFactory.defaultInstance().addViolation(ctx, r, n, "specificdescription", new Object[0]); assertTrue(ctx.getReport().isEmpty()); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java index f45be594d9..45d3571c5c 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java @@ -33,23 +33,23 @@ public class RuleViolationComparatorTest { int index = 0; // Different begin line - expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 10, "desc1", 0, 20, 80); - expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 20, "desc1", 0, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 10, "desc1", 1, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 20, "desc1", 1, 20, 80); // Different description - expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc1", 0, 20, 80); - expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc2", 0, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc1", 1, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc2", 1, 20, 80); // Different begin column - expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 0, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 1, 20, 80); expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 10, 20, 80); // Different end line - expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 0, 20, 80); - expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 0, 30, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 1, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 1, 30, 80); // Different end column - expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 0, 20, 80); - expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 0, 20, 90); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 1, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 1, 20, 90); // Different rule name - expectedOrder[index++] = createJavaRuleViolation(rule1, "file6", 10, "desc1", 0, 20, 80); - expectedOrder[index++] = createJavaRuleViolation(rule2, "file6", 10, "desc1", 0, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule1, "file6", 10, "desc1", 1, 20, 80); + expectedOrder[index++] = createJavaRuleViolation(rule2, "file6", 10, "desc1", 1, 20, 80); // Randomize List ruleViolations = new ArrayList<>(Arrays.asList(expectedOrder)); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java index 4eaed2cfa4..d9717e3e4c 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java @@ -55,11 +55,11 @@ public class RuleViolationTest { RuleContext ctx = new RuleContext(); ctx.setSourceCodeFile(new File("filename1")); DummyNode s = new DummyNode(); - s.setCoords(10, 1, 2, 3); + s.setCoords(10, 1, 11, 3); RuleViolation r1 = new ParametricRuleViolation(rule, ctx, s, "description"); ctx.setSourceCodeFile(new File("filename2")); DummyNode s1 = new DummyNode(); - s.setCoords(10, 1, 2, 3); + s1.setCoords(10, 1, 11, 3); RuleViolation r2 = new ParametricRuleViolation(rule, ctx, s1, "description"); assertEquals(-1, comp.compare(r1, r2)); assertEquals(1, comp.compare(r2, r1)); @@ -74,7 +74,7 @@ public class RuleViolationTest { DummyNode s = new DummyNode(); s.setCoords(10, 1, 15, 10); DummyNode s1 = new DummyNode(); - s.setCoords(20, 1, 25, 10); + s1.setCoords(20, 1, 25, 10); RuleViolation r1 = new ParametricRuleViolation(rule, ctx, s, "description"); RuleViolation r2 = new ParametricRuleViolation(rule, ctx, s1, "description"); assertTrue(comp.compare(r1, r2) < 0); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/jaxen/MatchesFunctionTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/jaxen/MatchesFunctionTest.java index 2725ecf6bb..0b8f5152d5 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/jaxen/MatchesFunctionTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/jaxen/MatchesFunctionTest.java @@ -13,13 +13,13 @@ import org.jaxen.Context; import org.jaxen.FunctionCallException; import org.junit.Test; -import net.sourceforge.pmd.lang.ast.impl.AbstractNode; +import net.sourceforge.pmd.lang.ast.impl.AbstractNodeWithTextCoordinates; import net.sourceforge.pmd.lang.ast.xpath.Attribute; import net.sourceforge.pmd.lang.xpath.MatchesFunction; public class MatchesFunctionTest { - public static class MyNode extends AbstractNode { + public static class MyNode extends AbstractNodeWithTextCoordinates { private String className; public MyNode() { diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java index ac2d4f86d6..0c2994908a 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/internal/NodeStreamBlanketTest.java @@ -8,6 +8,7 @@ import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertSame; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.node; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.nodeB; +import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.root; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.tree; import java.util.ArrayList; @@ -41,7 +42,8 @@ public class NodeStreamBlanketTest { private static final List ASTS = Arrays.asList( tree( () -> - node( + root( + node( node(), nodeB( @@ -55,7 +57,7 @@ public class NodeStreamBlanketTest { ), tree( () -> - node( + root( node(), node(), nodeB( 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 c05d5ea2c6..10d5af03d1 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 @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.ast.internal; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.followPath; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.node; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.pathsOf; +import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.root; import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.tree; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; @@ -25,6 +26,7 @@ import org.junit.Test; import net.sourceforge.pmd.lang.ast.DummyNode; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.NodeStream; +import net.sourceforge.pmd.lang.ast.NodeStream.DescendantNodeStream; /** @@ -35,7 +37,7 @@ public class NodeStreamTest { private final DummyNode tree1 = tree( () -> - node(// "" + root(// "" node(// 0 node(), // 00 node(// 01 @@ -52,7 +54,7 @@ public class NodeStreamTest { private final DummyNode tree2 = tree( () -> - node( + root( node(), node(), node( @@ -186,7 +188,7 @@ public class NodeStreamTest { @Test public void testGet() { // ("0", "00", "01", "010", "011", "012", "013", "1") - NodeStream stream = tree1.descendants(); + DescendantNodeStream stream = tree1.descendants(); assertEquals("0", stream.get(0).getImage()); assertEquals("00", stream.get(1).getImage()); @@ -197,7 +199,7 @@ public class NodeStreamTest { @Test public void testNodeStreamsCanBeIteratedSeveralTimes() { - NodeStream stream = tree1.descendants(); + DescendantNodeStream stream = tree1.descendants(); assertThat(stream.count(), equalTo(8)); assertThat(stream.count(), equalTo(8)); @@ -252,7 +254,7 @@ public class NodeStreamTest { MutableInt upstreamEvals = new MutableInt(); MutableInt downstreamEvals = new MutableInt(); - NodeStream stream = + NodeStream stream = tree1.descendants() .filter(n -> n.getImage().matches("0.*")) .take(4) @@ -299,7 +301,7 @@ public class NodeStreamTest { MutableInt tree1Evals = new MutableInt(); - NodeStream unionStream = tree1.descendantsOrSelf().peek(n -> tree1Evals.increment()); + NodeStream unionStream = tree1.descendantsOrSelf().peek(n -> tree1Evals.increment()); int i = 0; diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java index 8369229fe3..8017a09fd6 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java @@ -37,34 +37,33 @@ public class AttributeAxisIteratorTest { AttributeAxisIterator it = new AttributeAxisIterator(dummyNode); Map atts = toMap(it); - Assert.assertEquals(7, atts.size()); + Assert.assertEquals(6, atts.size()); assertTrue(atts.containsKey("BeginColumn")); assertTrue(atts.containsKey("BeginLine")); assertTrue(atts.containsKey("FindBoundary")); assertTrue(atts.containsKey("Image")); - assertTrue(atts.containsKey("SingleLine")); assertTrue(atts.containsKey("EndColumn")); assertTrue(atts.containsKey("EndLine")); } @Test public void testAttributeAxisIteratorWithEnum() { - DummyNodeWithEnum dummyNode = new DummyNodeWithEnum(1); + DummyNodeWithEnum dummyNode = new DummyNodeWithEnum(); AttributeAxisIterator it = new AttributeAxisIterator(dummyNode); Map atts = toMap(it); - Assert.assertEquals(8, atts.size()); + Assert.assertEquals(7, atts.size()); assertTrue(atts.containsKey("Enum")); assertEquals(DummyNodeWithEnum.MyEnum.FOO, atts.get("Enum").getValue()); } @Test public void testAttributeAxisIteratorWithList() { - DummyNodeWithList dummyNode = new DummyNodeWithList(1); + DummyNodeWithList dummyNode = new DummyNodeWithList(); AttributeAxisIterator it = new AttributeAxisIterator(dummyNode); Map atts = toMap(it); - Assert.assertEquals(8, atts.size()); + Assert.assertEquals(7, atts.size()); assertTrue(atts.containsKey("List")); assertEquals(Arrays.asList("A", "B"), atts.get("List").getValue()); assertFalse(atts.containsKey("NodeList")); @@ -81,10 +80,6 @@ public class AttributeAxisIteratorTest { public static class DummyNodeWithEnum extends DummyNode { - public DummyNodeWithEnum(int id) { - super(); - } - public enum MyEnum { FOO, BAR } @@ -96,10 +91,6 @@ public class AttributeAxisIteratorTest { public static class DummyNodeWithList extends DummyNode { - public DummyNodeWithList(int id) { - super(); - } - public List getList() { return Arrays.asList("A", "B"); } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/NoAttributeTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/NoAttributeTest.java index a6279e5922..9f9b93e3f6 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/NoAttributeTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/NoAttributeTest.java @@ -25,7 +25,7 @@ public class NoAttributeTest { @Test public void testNoAttrInherited() { - Node child = new NodeNoInherited(12); + Node child = new NodeNoInherited(); Set attrNames = IteratorUtil.toList(child.getXPathAttributesIterator()).stream().map(Attribute::getName).collect(Collectors.toSet()); @@ -45,7 +45,7 @@ public class NoAttributeTest { assertTrue(0 < IteratorUtil.count(new NodeAllAttr(12).getXPathAttributesIterator())); - NodeNoAttrAll child = new NodeNoAttrAll(12); + NodeNoAttrAll child = new NodeNoAttrAll(); Set attrNames = IteratorUtil.toList(child.getXPathAttributesIterator()).stream().map(Attribute::getName).collect(Collectors.toSet()); // from Noded, so not suppressed @@ -57,7 +57,7 @@ public class NoAttributeTest { @Test public void testNoAttrAllIsNotInherited() { - NodeNoAttrAllChild child = new NodeNoAttrAllChild(12); + NodeNoAttrAllChild child = new NodeNoAttrAllChild(); Set attrNames = IteratorUtil.toList(child.getXPathAttributesIterator()).stream().map(Attribute::getName).collect(Collectors.toSet()); @@ -70,14 +70,10 @@ public class NoAttributeTest { private static class DummyNodeParent extends DummyNode { - DummyNodeParent(int id) { + DummyNodeParent() { super(); } - DummyNodeParent(int id, boolean findBoundary) { - super(findBoundary); - } - public String getSomeName() { return "Foo"; } @@ -100,10 +96,6 @@ public class NoAttributeTest { @NoAttribute(scope = NoAttrScope.INHERITED) private static class NodeNoInherited extends DummyNodeParent { - NodeNoInherited(int id) { - super(id); - } - // getSomeName is inherited and filtered out by NoAttrScope.INHERITED // getSomeInt is inherited but overridden here, so NoAttrScope.INHERITED has no effect // getSomeLong is inherited and overridden here, @@ -140,18 +132,13 @@ public class NoAttributeTest { private static class NodeAllAttr extends DummyNodeParent { NodeAllAttr(int id) { - super(id); + super(); } } @NoAttribute(scope = NoAttrScope.ALL) private static class NodeNoAttrAll extends DummyNodeParent { - - NodeNoAttrAll(int id) { - super(id); - } - public int getMySuppressedAttr() { return 12; } @@ -161,11 +148,6 @@ public class NoAttributeTest { private static class NodeNoAttrAllChild extends NodeNoAttrAll { - - NodeNoAttrAllChild(int id) { - super(id); - } - public int getNotSuppressedAttr() { return 12; } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/XPathRuleTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/XPathRuleTest.java index 83e85788c2..41888278c7 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/XPathRuleTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/XPathRuleTest.java @@ -94,7 +94,7 @@ public class XPathRuleTest { DummyRoot root = new DummyRoot(); DummyNode dummy = new DummyNodeWithDeprecatedAttribute(2); dummy.setCoords(1, 1, 1, 2); - root.jjtAddChild(dummy, 0); + root.addChild(dummy, 0); return root; } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/saxon/ElementNodeTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/saxon/ElementNodeTest.java index 4448d58766..0a4cf00195 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/saxon/ElementNodeTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/saxon/ElementNodeTest.java @@ -19,7 +19,7 @@ public class ElementNodeTest { DummyNode foo1 = new DummyNode(false, "foo"); foo1.setCoords(1, 1, 2, 2); DummyNode foo2 = new DummyNode(false, "foo"); - foo1.setCoords(2, 1, 2, 2); + foo2.setCoords(2, 1, 2, 2); node.addChild(foo1, 0); node.addChild(foo2, 1); diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractEcmascriptNode.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractEcmascriptNode.java index 2000f9c8fc..a58eb736f8 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractEcmascriptNode.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractEcmascriptNode.java @@ -14,10 +14,13 @@ abstract class AbstractEcmascriptNode extends AbstractNodeWit protected final T node; AbstractEcmascriptNode(T node) { - super(); this.node = node; } + protected void addChild(AbstractEcmascriptNode child, int index) { + super.addChild(child, index); + } + /* package private */ void calculateLineNumbers(SourceCodePositioner positioner) { int startOffset = node.getAbsolutePosition(); diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserVisitor.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserVisitor.java index 8fe3dbf9ea..175c3a6789 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserVisitor.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserVisitor.java @@ -1,8 +1,8 @@ /* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ -package net.sourceforge.pmd.lang.ecmascript.ast; +package net.sourceforge.pmd.lang.ecmascript.ast; public interface EcmascriptParserVisitor { diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java index 992e23500e..5fd044d849 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java @@ -66,7 +66,6 @@ import org.mozilla.javascript.ast.XmlExpression; import org.mozilla.javascript.ast.XmlMemberGet; import org.mozilla.javascript.ast.XmlString; -import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.SourceCodePositioner; final class EcmascriptTreeBuilder implements NodeVisitor { @@ -130,7 +129,7 @@ final class EcmascriptTreeBuilder implements NodeVisitor { private final Map> parseProblemToNode = new HashMap<>(); // The nodes having children built. - private final Stack nodes = new Stack<>(); + private final Stack> nodes = new Stack<>(); // The Rhino nodes with children to build. private final Stack parents = new Stack<>(); @@ -187,10 +186,9 @@ final class EcmascriptTreeBuilder implements NodeVisitor { AbstractEcmascriptNode node = createNodeAdapter(astNode); // Append to parent - Node parent = nodes.isEmpty() ? null : nodes.peek(); + AbstractEcmascriptNode parent = nodes.isEmpty() ? null : nodes.peek(); if (parent != null) { - parent.jjtAddChild(node, parent.getNumChildren()); - node.jjtSetParent(parent); + parent.addChild(node, parent.getNumChildren()); } handleParseProblems(node); diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt index 8504e4f253..8f85343d01 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt @@ -19,13 +19,6 @@ import java.util.* // kotlin converts getters of java types into property accessors // but it doesn't recognise jjtGet* methods as getters - -val AbstractJjtreeNode<*>.firstToken: JavaccToken - get() = jjtGetFirstToken() - -val AbstractJjtreeNode<*>.lastToken: JavaccToken - get() = jjtGetLastToken() - fun Node.safeGetChild(i: Int): Node? = when { i < numChildren -> getChild(i) else -> null diff --git a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/AbstractScalaNode.java b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/AbstractScalaNode.java index 224b90876a..240ec21cd0 100644 --- a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/AbstractScalaNode.java +++ b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/AbstractScalaNode.java @@ -32,6 +32,10 @@ abstract class AbstractScalaNode extends AbstractNode child, int index) { + super.addChild(child, index); + } + @Override public boolean isImplicit() { return pos.end() - pos.start() == 0; diff --git a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaTreeBuilder.java b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaTreeBuilder.java index 6a664d19a6..32a704d91f 100644 --- a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaTreeBuilder.java +++ b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaTreeBuilder.java @@ -10,8 +10,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Stack; -import net.sourceforge.pmd.lang.ast.Node; - import scala.meta.Case; import scala.meta.Ctor; import scala.meta.Decl; @@ -173,7 +171,7 @@ class ScalaTreeBuilder { } // The nodes having children built. - private final Stack nodes = new Stack<>(); + private final Stack> nodes = new Stack<>(); private static void register(Class nodeType, Class> nodeAdapterType) { @@ -185,10 +183,10 @@ class ScalaTreeBuilder { } @SuppressWarnings("unchecked") - private static ScalaNode createNodeAdapter(T node) { + private static AbstractScalaNode createNodeAdapter(T node) { try { - Constructor> constructor = null; + Constructor> constructor = null; // This isInstance is unfortunately necessary as Scala gives us // access to the Interface (Trait) of classes at compile time, but @@ -197,7 +195,7 @@ class ScalaTreeBuilder { // translation between Scala Traits and Java Classes for (Class treeClass : NODE_TYPE_TO_NODE_ADAPTER_TYPE.keySet()) { if (treeClass.isInstance(node)) { - constructor = (Constructor>) NODE_TYPE_TO_NODE_ADAPTER_TYPE.get(treeClass); + constructor = (Constructor>) NODE_TYPE_TO_NODE_ADAPTER_TYPE.get(treeClass); } } @@ -229,12 +227,11 @@ class ScalaTreeBuilder { private ScalaNode buildInternal(T astNode) { // Create a Node - ScalaNode node = createNodeAdapter(astNode); + AbstractScalaNode node = createNodeAdapter(astNode); // Append to parent - Node parent = nodes.isEmpty() ? null : nodes.peek(); + AbstractScalaNode parent = nodes.isEmpty() ? null : nodes.peek(); if (parent != null) { - parent.jjtAddChild(node, parent.getNumChildren()); - node.jjtSetParent(parent); + parent.addChild(node, parent.getNumChildren()); } // Build the children... diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java index ecdfaeb571..6ca32ca31d 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java @@ -76,9 +76,8 @@ public class DummyLanguageModule extends BaseLanguageModule { return new AbstractParser(parserOptions) { @Override public Node parse(String fileName, Reader source) throws ParseException { - DummyNode node = new DummyRootNode(1); - node.testingOnlySetBeginLine(1); - node.testingOnlySetBeginColumn(1); + DummyNode node = new DummyRootNode(); + node.setCoords(1, 1, 1, 2); node.setImage("Foo"); return node; } @@ -89,10 +88,6 @@ public class DummyLanguageModule extends BaseLanguageModule { private static class DummyRootNode extends DummyNode implements RootNode { - DummyRootNode(int id) { - super(); - } - } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java index 74c76dec6e..583d1d3db8 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java @@ -8,13 +8,17 @@ import net.sourceforge.pmd.lang.ast.impl.AbstractNodeWithTextCoordinates; public class DummyNode extends AbstractNodeWithTextCoordinates { + @Override + public void setCoords(int bline, int bcol, int eline, int ecol) { + super.setCoords(bline, bcol, eline, ecol); + } + @Deprecated @Override public String toString() { return "dummyNode"; } - @Override public String getXPathNodeName() { return "dummyNode"; diff --git a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java index 4aa1261628..815d3562ce 100644 --- a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java +++ b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java @@ -65,10 +65,8 @@ public class RuleTstTest { Mockito.doAnswer(new Answer() { private RuleViolation createViolation(RuleContext context, int beginLine, String message) { DummyNode node = new DummyNode(); - node.testingOnlySetBeginLine(beginLine); - node.testingOnlySetBeginColumn(1); - ParametricRuleViolation violation = new ParametricRuleViolation(rule, context, node, message); - return violation; + node.setCoords(beginLine, 1, beginLine + 1, 2); + return new ParametricRuleViolation(rule, context, node, message); } @Override diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java index 3f59f75e68..f7d0444916 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java @@ -29,16 +29,6 @@ abstract class AbstractVmNode extends AbstractJjtreeNode implements VmNo super(i); } - @Override - public JavaccToken getFirstToken() { - return this.getFirstToken(); - } - - @Override - public JavaccToken getLastToken() { - return this.getLastToken(); - } - @Override public String getXPathNodeName() { return VmParserImplTreeConstants.jjtNodeName[id];