From 2fad9bf507d5c0c4a022ce742b63b727471a4dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 15 Nov 2020 16:36:44 +0100 Subject: [PATCH] Changes to DataSource and test modules --- .../java/net/sourceforge/pmd/lang/Parser.java | 2 +- .../pmd/util/datasource/DataSource.java | 33 +++++++++++ .../pmd/lang/ast/test/BaseParsingHelper.kt | 11 ++-- .../pmd/lang/ParserOptionsTest.java | 11 ---- .../pmd/test/lang/DummyLanguageModule.java | 56 +++++++++---------- 5 files changed, 67 insertions(+), 46 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java index e808c9d130..4827ce34a6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java @@ -1,4 +1,4 @@ -/** +/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/DataSource.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/DataSource.java index 914f6c73bc..3338c64a8a 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/DataSource.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/DataSource.java @@ -7,6 +7,14 @@ package net.sourceforge.pmd.util.datasource; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.nio.charset.Charset; + +import org.apache.commons.io.ByteOrderMark; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.BOMInputStream; /** * Represents a source file to be analyzed. Different implementations can get @@ -14,6 +22,7 @@ import java.io.InputStream; * etc. */ public interface DataSource extends Closeable { + /** * Get an InputStream on the source file. * @@ -33,4 +42,28 @@ public interface DataSource extends Closeable { * @return String */ String getNiceFileName(boolean shortNames, String inputFileName); + + + static DataSource forString(String sourceText, String fileName) { + return new ReaderDataSource(new StringReader(sourceText), fileName); + } + + /** + * Reads the contents of the data source to a string. Skips the byte-order + * mark if present. Parsers expect input without a BOM. + * + * @param dataSource Data source + * @param sourceEncoding Encoding to use to read from the data source + */ + static String readToString(DataSource dataSource, Charset sourceEncoding) throws IOException { + String fullSource; + try (InputStream stream = dataSource.getInputStream(); + // Skips the byte-order mark + BOMInputStream bomIs = new BOMInputStream(stream, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE); + Reader reader = new InputStreamReader(bomIs, sourceEncoding)) { + + fullSource = IOUtils.toString(reader); // this already buffers properly + } + return fullSource; + } } diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/BaseParsingHelper.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/BaseParsingHelper.kt index 1e1040fdae..02318cb1ea 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/BaseParsingHelper.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/BaseParsingHelper.kt @@ -5,10 +5,8 @@ package net.sourceforge.pmd.lang.ast.test import net.sourceforge.pmd.* import net.sourceforge.pmd.lang.* -import net.sourceforge.pmd.lang.ast.AstAnalysisContext -import net.sourceforge.pmd.lang.ast.AstProcessingStage -import net.sourceforge.pmd.lang.ast.Node -import net.sourceforge.pmd.lang.ast.RootNode +import net.sourceforge.pmd.lang.ast.* +import net.sourceforge.pmd.util.datasource.DataSource import org.apache.commons.io.IOUtils import java.io.File import java.io.InputStream @@ -123,7 +121,10 @@ abstract class BaseParsingHelper, T : RootNode val handler = lversion.languageVersionHandler val options = params.parserOptions ?: handler.defaultParserOptions val parser = handler.getParser(options) - val rootNode = rootClass.cast(parser.parse(null, StringReader(sourceCode))) + val source = DataSource.forString(sourceCode, FileAnalysisException.NO_FILE_NAME) + val toString = DataSource.readToString(source, StandardCharsets.UTF_8) + val task = Parser.ParserTask(lversion, FileAnalysisException.NO_FILE_NAME, toString, SemanticErrorReporter.noop(), options.suppressMarker) + val rootNode = rootClass.cast(parser.parse(task)) if (params.doProcess) { postProcessing(handler, lversion, rootNode) } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java b/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java index 945946df05..2027fa82b3 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/lang/ParserOptionsTest.java @@ -12,17 +12,6 @@ import org.junit.Test; */ public class ParserOptionsTest { - /** - * SuppressMarker should be initially null and changeable. - */ - @Test - public void testSuppressMarker() { - ParserOptions parserOptions = new ParserOptions(); - Assert.assertNull(parserOptions.getSuppressMarker()); - parserOptions.setSuppressMarker("foo"); - Assert.assertEquals("foo", parserOptions.getSuppressMarker()); - } - /** * Verify that the equals and hashCode methods work as expected. */ 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 b3c99b6e4c..acd12a7e7c 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 @@ -4,21 +4,12 @@ package net.sourceforge.pmd.test.lang; -import java.io.Reader; - -import org.checkerframework.checker.nullness.qual.NonNull; - -import net.sourceforge.pmd.Rule; -import net.sourceforge.pmd.RuleViolation; -import net.sourceforge.pmd.lang.AbstractParser; import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler; import net.sourceforge.pmd.lang.BaseLanguageModule; +import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.lang.Parser; import net.sourceforge.pmd.lang.ParserOptions; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.ParseException; import net.sourceforge.pmd.lang.ast.RootNode; -import net.sourceforge.pmd.lang.rule.ParametricRuleViolation; import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory; import net.sourceforge.pmd.test.lang.ast.DummyNode; @@ -51,34 +42,41 @@ public class DummyLanguageModule extends BaseLanguageModule { @Override public Parser getParser(ParserOptions parserOptions) { - return new AbstractParser(parserOptions) { - @Override - public DummyRootNode parse(String fileName, Reader source) throws ParseException { - DummyRootNode node = new DummyRootNode(); - node.setCoords(1, 1, 1, 2); - node.setImage("Foo"); - return node; - } - + return task -> { + DummyRootNode node = new DummyRootNode(); + node.setCoords(1, 1, 1, 2); + node.setLanguageVersion(task.getLanguageVersion()); + node.setImage("Foo"); + return node; }; } } public static class DummyRootNode extends DummyNode implements RootNode { + + private LanguageVersion languageVersion; + + @Override + public LanguageVersion getLanguageVersion() { + return languageVersion; + } + + @Override + public String getSourceCodeFile() { + return "someFile.dummy"; + } + + public DummyRootNode setLanguageVersion(LanguageVersion languageVersion) { + this.languageVersion = languageVersion; + return this; + } + + } public static class RuleViolationFactory extends DefaultRuleViolationFactory { - @Override - public RuleViolation createViolation(Rule rule, @NonNull Node location, @NonNull String filename, @NonNull String formattedMessage) { - return new ParametricRuleViolation(rule, filename, location, formattedMessage) { - @Override - public String getPackageName() { - this.packageName = "foo"; // just for testing variable expansion - return super.getPackageName(); - } - }; - } + } }