From af576dbf78dc4795fb0350c7ee610098f42efd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 27 Feb 2022 18:56:49 +0100 Subject: [PATCH] PR review --- docs/pages/release_notes.md | 1 + .../main/java/net/sourceforge/pmd/PMD.java | 2 +- .../java/net/sourceforge/pmd/PmdAnalysis.java | 4 +- .../pmd/benchmark/Benchmarker.java | 86 +++++++++---------- .../pmd/lang/document/FileCollector.java | 6 +- .../net/sourceforge/pmd/cli/BaseCLITest.java | 22 +++-- 6 files changed, 65 insertions(+), 56 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 6717b274e9..108c2edbee 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -36,6 +36,7 @@ try (PmdAnalysis pmd = PmdAnalysis.create(config)) { The `PMD` class still supports methods related to CLI execution: `runPmd` and `main`. All other members are now deprecated for removal. +The CLI itself remains compatible, if you run PMD via command-line, no action is required on your part. ### Fixed Issues diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java index 190ab2fa84..70569d89c6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -56,7 +56,7 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger; * Entry point for PMD's CLI. Use {@link #runPmd(PMDConfiguration)} * or {@link #runPmd(String...)} to mimic a CLI run. This class is * not a supported programmatic API anymore, use {@link PmdAnalysis} - * for fine control over the PMD execution. + * for fine control over the PMD integration and execution. * *

Warning: This class is not intended to be instantiated or subclassed. It will * be made final in PMD7. diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java index 45973be69b..6db4e1c5af 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java @@ -49,6 +49,7 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger; * config.setReportFormat("xml"); * * try (PmdAnalysis pmd = PmdAnalysis.create(config)) { + * // here you can add more rulesets or files * pmd.performAnalysis(); * } * } @@ -128,7 +129,8 @@ public final class PmdAnalysis implements AutoCloseable { } /** - * Add a new renderer. The given renderer must already be started. + * Add a new renderer. The given renderer must not already be started, + * it will be started by {@link #performAnalysis()}. * * @throws NullPointerException If the parameter is null */ diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java b/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java index 3c96f52847..326d3898bf 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java @@ -4,15 +4,17 @@ package net.sourceforge.pmd.benchmark; +import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; -import java.io.StringReader; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; -import java.util.logging.Logger; import org.apache.commons.lang3.StringUtils; @@ -29,13 +31,12 @@ import net.sourceforge.pmd.RulesetsFactoryUtils; import net.sourceforge.pmd.SourceCodeProcessor; import net.sourceforge.pmd.lang.AbstractParser; import net.sourceforge.pmd.lang.Language; +import net.sourceforge.pmd.lang.LanguageFilenameFilter; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.LanguageVersion; -import net.sourceforge.pmd.lang.LanguageVersionDiscoverer; import net.sourceforge.pmd.lang.Parser; -import net.sourceforge.pmd.lang.document.FileCollector; -import net.sourceforge.pmd.lang.document.TextFile; -import net.sourceforge.pmd.util.log.SimplePmdLogger; +import net.sourceforge.pmd.util.FileUtil; +import net.sourceforge.pmd.util.datasource.DataSource; /** * @deprecated use {@link TimeTracker} instead @@ -43,7 +44,6 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger; @Deprecated public final class Benchmarker { - private static final Logger LOGGER = Logger.getLogger(Benchmarker.class.getName()); private static final Map BENCHMARKS_BY_NAME = new HashMap<>(); private Benchmarker() { } @@ -96,42 +96,40 @@ public final class Benchmarker { String targetjdk = findOptionalStringValue(args, "--targetjdk", "1.4"); Language language = LanguageRegistry.getLanguage("Java"); LanguageVersion languageVersion = language.getVersion(targetjdk); - LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer(); - if (languageVersion != null) { - discoverer.setDefaultLanguageVersion(languageVersion); + if (languageVersion == null) { + languageVersion = language.getDefaultVersion(); } String srcDir = findOptionalStringValue(args, "--source-directory", "/usr/local/java/src/java/lang/"); + List dataSources = FileUtil.collectFiles(srcDir, new LanguageFilenameFilter(language)); - try (FileCollector collector = FileCollector.newCollector(discoverer, new SimplePmdLogger(LOGGER))) { - boolean debug = findBooleanSwitch(args, "--debug"); - boolean parseOnly = findBooleanSwitch(args, "--parse-only"); + boolean debug = findBooleanSwitch(args, "--debug"); + boolean parseOnly = findBooleanSwitch(args, "--parse-only"); + if (debug) { + System.out.println("Using " + language.getName() + " " + languageVersion.getVersion()); + } + if (parseOnly) { + Parser parser = PMD.parserFor(languageVersion, null); + parseStress(parser, dataSources, debug); + } else { + String ruleset = findOptionalStringValue(args, "--ruleset", ""); if (debug) { - System.out.println("Using " + language.getName() + " " + languageVersion.getVersion()); + System.out.println("Checking directory " + srcDir); } - if (parseOnly) { - Parser parser = PMD.parserFor(languageVersion, null); - parseStress(parser, collector, debug); + Set results = new TreeSet<>(); + RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory(); + if (StringUtils.isNotBlank(ruleset)) { + stress(languageVersion, factory.createRuleSet(ruleset), dataSources, results, debug); } else { - String ruleset = findOptionalStringValue(args, "--ruleset", ""); - if (debug) { - System.out.println("Checking directory " + srcDir); + Iterator i = factory.getRegisteredRuleSets(); + while (i.hasNext()) { + stress(languageVersion, i.next(), dataSources, results, debug); } - Set results = new TreeSet<>(); - RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory(); - if (StringUtils.isNotBlank(ruleset)) { - stress(languageVersion, factory.createRuleSet(ruleset), collector, results, debug); - } else { - Iterator i = factory.getRegisteredRuleSets(); - while (i.hasNext()) { - stress(languageVersion, i.next(), collector, results, debug); - } - } - - TextReport report = new TextReport(); - report.generate(results, System.err); } + + TextReport report = new TextReport(); + report.generate(results, System.err); } } @@ -144,13 +142,14 @@ public final class Benchmarker { * boolean * @throws IOException */ - private static void parseStress(Parser parser, FileCollector dataSources, boolean debug) throws IOException { + private static void parseStress(Parser parser, List dataSources, boolean debug) throws IOException { long start = System.currentTimeMillis(); - for (TextFile ds : dataSources.getCollectedFiles()) { - String contents = ds.readContents(); - AbstractParser.doParse(parser, ds.getDisplayName(), new StringReader(contents)); + for (DataSource ds : dataSources) { + try (DataSource dataSource = ds; InputStreamReader reader = new InputStreamReader(dataSource.getInputStream())) { + AbstractParser.doParse(parser, dataSource.getNiceFileName(false, null), reader); + } } if (debug) { @@ -165,7 +164,7 @@ public final class Benchmarker { * LanguageVersion * @param ruleSet * RuleSet - * @param files + * @param dataSources * List * @param results * Set @@ -174,7 +173,7 @@ public final class Benchmarker { * @throws PMDException * @throws IOException */ - private static void stress(LanguageVersion languageVersion, RuleSet ruleSet, FileCollector files, + private static void stress(LanguageVersion languageVersion, RuleSet ruleSet, List dataSources, Set results, boolean debug) throws PMDException, IOException { for (Rule rule: ruleSet.getRules()) { @@ -190,10 +189,11 @@ public final class Benchmarker { RuleContext ctx = new RuleContext(); long start = System.currentTimeMillis(); - for (TextFile ds : files.getCollectedFiles()) { - String source = ds.readContents(); - ctx.setSourceCodeFile(new File(ds.getPathId())); - new SourceCodeProcessor(config).processSourceCode(new StringReader(source), ruleSets, ctx); + for (DataSource ds : dataSources) { + try (DataSource dataSource = ds; InputStream stream = new BufferedInputStream(dataSource.getInputStream())) { + ctx.setSourceCodeFile(new File(dataSource.getNiceFileName(false, null))); + new SourceCodeProcessor(config).processSourceCode(stream, ruleSets, ctx); + } } long end = System.currentTimeMillis(); long elapsed = end - start; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FileCollector.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FileCollector.java index 4c72418292..bd2ec09441 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FileCollector.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FileCollector.java @@ -29,7 +29,6 @@ import java.util.Objects; import java.util.Set; import net.sourceforge.pmd.PmdAnalysis; -import net.sourceforge.pmd.annotation.Experimental; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.internal.util.AssertionUtil; import net.sourceforge.pmd.lang.Language; @@ -63,9 +62,10 @@ public final class FileCollector implements AutoCloseable { } /** - * Experimental: please use {@link PmdAnalysis#files()} instead of this + * Internal API: please use {@link PmdAnalysis#files()} instead of + * creating a collector yourself. */ - @Experimental + @InternalApi public static FileCollector newCollector(LanguageVersionDiscoverer discoverer, PmdLogger logger) { return new FileCollector(discoverer, logger); } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java index e6544c5d9c..a4370762c0 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java @@ -107,14 +107,20 @@ public abstract class BaseCLITest { * Returns the log output. */ protected String runTest(int expectedExitCode, String... args) { - ByteArrayOutputStream console = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(console); - System.setOut(out); - System.setErr(out); - - StatusCode statusCode = PMD.runPmd(args); - assertEquals(expectedExitCode, statusCode.toInt()); - return console.toString(); + PrintStream oldOut = System.out; + PrintStream oldErr = System.err; + try { + ByteArrayOutputStream console = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(console); + System.setOut(out); + System.setErr(out); + StatusCode statusCode = PMD.runPmd(args); + assertEquals(expectedExitCode, statusCode.toInt()); + return console.toString(); + } finally { + System.setOut(oldOut); + System.setOut(oldErr); + } } @Deprecated