From 39b51aac473512fd936d3255332139310d499b82 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 19 Aug 2019 12:27:44 -0400 Subject: [PATCH] code review fixes: re-enable javadocs ScalaTokenizer keeps nodes in scala for performance gain Change AST{name}Node designation to {name} --- pmd-scala/pom.xml | 16 --------- .../sourceforge/pmd/cpd/ScalaTokenizer.java | 35 +++++++++---------- .../pmd/lang/scala/ast/ScalaWrapperNode.java | 2 +- .../pmd/scala/ScalaParserTest.java | 6 ++-- .../pmd/scala/rule/XPathRuleTest.java | 2 +- 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/pmd-scala/pom.xml b/pmd-scala/pom.xml index eba90db080..563ff485ac 100644 --- a/pmd-scala/pom.xml +++ b/pmd-scala/pom.xml @@ -35,22 +35,6 @@ pmd-scala-checkstyle-suppressions.xml - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - attach-javadocs - none - - jar - - - - diff --git a/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaTokenizer.java b/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaTokenizer.java index e456755c9b..38a813592b 100644 --- a/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaTokenizer.java +++ b/pmd-scala/src/main/java/net/sourceforge/pmd/cpd/ScalaTokenizer.java @@ -5,8 +5,6 @@ package net.sourceforge.pmd.cpd; import java.io.IOException; -import java.util.Iterator; -import java.util.List; import java.util.Properties; import org.apache.commons.lang3.StringUtils; @@ -16,10 +14,11 @@ import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.lang.scala.ScalaLanguageHandler; import net.sourceforge.pmd.lang.scala.ScalaLanguageModule; -import scala.collection.JavaConverters; +import scala.collection.Iterator; import scala.meta.Dialect; import scala.meta.inputs.Input; import scala.meta.internal.tokenizers.ScalametaTokenizer; +import scala.meta.tokens.Token; /** * Scala Tokenizer class. Uses the Scala Meta Tokenizer. @@ -69,11 +68,9 @@ public class ScalaTokenizer implements Tokenizer { // tokenize with a filter scala.meta.tokens.Tokens tokens = tokenizer.tokenize(); - List tokenList = JavaConverters.asJava(tokens.toList()); + ScalaTokenFilter filter = new ScalaTokenFilter(tokens.iterator()); - ScalaTokenFilter filter = new ScalaTokenFilter(tokenList); - - scala.meta.tokens.Token token; + Token token; while ((token = filter.getNextToken()) != null) { String tokenText = token.text() != null ? token.text() : token.name(); TokenEntry cpdToken = new TokenEntry(tokenText, filename, token.pos().startLine()); @@ -86,32 +83,32 @@ public class ScalaTokenizer implements Tokenizer { * and patterns. */ private static class ScalaTokenFilter { - Iterator tokenListIter; + Iterator tokenIter; - ScalaTokenFilter(List tokenList) { - this.tokenListIter = tokenList.iterator(); + ScalaTokenFilter(Iterator iterator) { + this.tokenIter = iterator.iterator(); } - scala.meta.tokens.Token getNextToken() { - if (!tokenListIter.hasNext()) { + Token getNextToken() { + if (!tokenIter.hasNext()) { return null; } - scala.meta.tokens.Token token; + Token token; do { - token = tokenListIter.next(); - } while (token != null && skipToken(token) && tokenListIter.hasNext()); + token = tokenIter.next(); + } while (token != null && skipToken(token) && tokenIter.hasNext()); return token; } - private boolean skipToken(scala.meta.tokens.Token token) { + private boolean skipToken(Token token) { boolean skip = false; if (token.text() != null) { // skip any token that is whitespaces - skip |= token instanceof scala.meta.tokens.Token.Space || token instanceof scala.meta.tokens.Token.Tab - || token instanceof scala.meta.tokens.Token.CR || token instanceof scala.meta.tokens.Token.LF - || token instanceof scala.meta.tokens.Token.FF || token instanceof scala.meta.tokens.Token.LFLF; + skip |= token instanceof Token.Space || token instanceof Token.Tab + || token instanceof Token.CR || token instanceof Token.LF + || token instanceof Token.FF || token instanceof Token.LFLF; } return skip; } diff --git a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaWrapperNode.java b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaWrapperNode.java index 659b565b38..08ab6e950c 100644 --- a/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaWrapperNode.java +++ b/pmd-scala/src/main/java/net/sourceforge/pmd/lang/scala/ast/ScalaWrapperNode.java @@ -60,7 +60,7 @@ public class ScalaWrapperNode extends AbstractNode implements ScalaNode { @Override public String getXPathNodeName() { - return "AST" + node.productPrefix().replace(".", "") + "Node"; + return node.productPrefix().replace(".", ""); } @Override diff --git a/pmd-scala/src/test/java/net/sourceforge/pmd/scala/ScalaParserTest.java b/pmd-scala/src/test/java/net/sourceforge/pmd/scala/ScalaParserTest.java index fc11bd88ed..5dca52542f 100644 --- a/pmd-scala/src/test/java/net/sourceforge/pmd/scala/ScalaParserTest.java +++ b/pmd-scala/src/test/java/net/sourceforge/pmd/scala/ScalaParserTest.java @@ -32,9 +32,9 @@ public class ScalaParserTest { new StringReader(IOUtils.toString(getClass().getResourceAsStream(SCALA_TEST), "UTF-8"))); final List nodeLineTargets = new ArrayList(); - nodeLineTargets.add(new NodeLineMatcher("ASTTermNameNode", "Main", 1)); - nodeLineTargets.add(new NodeLineMatcher("ASTTypeNameNode", "App", 1)); - nodeLineTargets.add(new NodeLineMatcher("ASTTermNameNode", "println", 2)); + nodeLineTargets.add(new NodeLineMatcher("TermName", "Main", 1)); + nodeLineTargets.add(new NodeLineMatcher("TypeName", "App", 1)); + nodeLineTargets.add(new NodeLineMatcher("TermName", "println", 2)); ScalaParserVisitorAdapter visitor = new ScalaParserVisitorAdapter() { public Object visit(ScalaNode node, Object data) { diff --git a/pmd-scala/src/test/java/net/sourceforge/pmd/scala/rule/XPathRuleTest.java b/pmd-scala/src/test/java/net/sourceforge/pmd/scala/rule/XPathRuleTest.java index 100e5761a6..1823761459 100644 --- a/pmd-scala/src/test/java/net/sourceforge/pmd/scala/rule/XPathRuleTest.java +++ b/pmd-scala/src/test/java/net/sourceforge/pmd/scala/rule/XPathRuleTest.java @@ -41,7 +41,7 @@ public class XPathRuleTest extends RuleTst { @Test public void testPrintHelloWorld() throws Exception { - String xpath = "//ASTTermApplyNode/ASTTermNameNode[@Image=\"println\"]"; + String xpath = "//TermApply/TermName[@Image=\"println\"]"; rule.setXPath(xpath); rule.setVersion(XPathRuleQuery.XPATH_2_0); Report report = getReportForTestString(rule,