From 3dde3bc7392404207e4f97c32427306328ad76d4 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 17 Feb 2022 12:27:00 +0100 Subject: [PATCH 1/2] Bump maven-pmd-plugin from 3.15.0 to 3.16.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 05dc27034c..71fd3df262 100644 --- a/pom.xml +++ b/pom.xml @@ -94,7 +94,7 @@ 3.0.0-M5 9.3 3.1.2 - 3.15.0 + 3.16.0 1.10.12 3.2.0 4.7.2 From 7733be75ff80a3ce5bdf8e7ca4f82c2483d99e57 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 18 Feb 2022 11:33:05 +0100 Subject: [PATCH 2/2] [core] More deprecations around PMD/Report/RuleContext (#3762) --- docs/pages/release_notes.md | 11 ++++++++++- .../src/main/java/net/sourceforge/pmd/PMD.java | 15 +++++++++++++++ .../main/java/net/sourceforge/pmd/Report.java | 11 +++++++++++ .../java/net/sourceforge/pmd/RuleContext.java | 8 +++----- .../renderers/AbstractAccumulatingRenderer.java | 13 +++++++++++-- .../net/sourceforge/pmd/ExcludeLinesTest.java | 8 +++++--- .../metrics/xpath/XPathMetricFunctionTest.java | 7 ++++--- .../pmd/lang/java/rule/XPathRuleTest.java | 6 ++++-- .../sourceforge/pmd/testframework/RuleTst.java | 16 +++++++++------- 9 files changed, 72 insertions(+), 23 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 957dbd0bec..d2639e7e0f 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -24,12 +24,21 @@ This is a {{ site.pmd.release_type }} release. #### Deprecated API Some API deprecations were performed in core PMD classes, to improve compatibility with PMD 7. -- {% jdoc core::Report %}: construction methods like addViolation or createReport +- {% jdoc core::Report %}: the constructor and other construction methods like addViolation or createReport - {% jdoc core::RuleContext %}: all constructors, getters and setters. A new set of stable methods, matching those in PMD 7, was added to replace the `addViolation` overloads of {% jdoc core::lang.rule.AbstractRule %}. In PMD 7, `RuleContext` will be the API to report violations, and it can already be used as such in PMD 6. +- The field {% jdoc core::PMD#configuration %} is unused and will be removed. + +#### Internal API + +Those APIs are not intended to be used by clients, and will be hidden or removed with PMD 7.0.0. +You can identify them with the `@InternalApi` annotation. You'll also get a deprecation warning. + - {% jdoc core::RuleSet %}: methods that serve to apply rules, including `apply`, `start`, `end`, `removeDysfunctionalRules` +- {% jdoc !!core::renderers.AbstractAccumulatingRenderer#renderFileReport(Report) %} is internal API + and should not be overridden in own renderers. #### Changed API 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 5ace69f0be..2158fafaab 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -61,6 +61,17 @@ import net.sourceforge.pmd.util.log.ScopedLogHandlersManager; * process is controlled via interactions with this class. A command line * interface is supported, as well as a programmatic API for integrating PMD * with other software such as IDEs and Ant. + * + *

Main entrypoints are: + *

+ * + *

Warning: This class is not intended to be instantiated or subclassed. It will + * be made final in PMD7. */ public class PMD { @@ -77,7 +88,11 @@ public class PMD { /** * Contains the configuration with which this PMD instance has been created. + * + * @deprecated this configuration field is unused and will be removed. The class + * {@link PMD} should not be instantiated or subclassed. */ + @Deprecated protected final PMDConfiguration configuration; private final SourceCodeProcessor rulesetsFileProcessor; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/Report.java b/pmd-core/src/main/java/net/sourceforge/pmd/Report.java index bb68511842..e93e2272fb 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/Report.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/Report.java @@ -58,6 +58,17 @@ public class Report implements Iterable { private long end; private final List suppressedRuleViolations = new ArrayList<>(); + /** + * @deprecated {@link Report} instances are created by PMD. There is no need + * to create a own report. This constructor will be hidden + * in PMD7. + */ + @Deprecated + @InternalApi + public Report() { // NOPMD - UnnecessaryConstructor + // TODO: should be package-private, you have to use a listener to build a report. + } + /** * Creates a new, initialized, empty report for the given file name. * diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleContext.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleContext.java index 10486377a0..2546825626 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleContext.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleContext.java @@ -250,15 +250,13 @@ public class RuleContext { * * @param filename * The file name. - * @deprecated This method will be removed. The file should only be - * set with {@link #setSourceCodeFile(File)}. Setting the filename here - * has no effect. + * @deprecated Internal API, removed in PMD 7 */ @Deprecated + @InternalApi public void setSourceCodeFilename(String filename) { // ignored, does nothing. - LOG.warning("The method RuleContext::setSourceCodeFilename(String) has been deprecated and will be removed." - + "Setting the filename here has no effect. Use RuleContext::setSourceCodeFile(File) instead."); + LOG.warning("The method RuleContext::setSourceCodeFilename(String) has been deprecated and will be removed."); } /** diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java index 5ffb4e8fd5..2c6dacefe4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.renderers; import java.io.IOException; import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.util.datasource.DataSource; /** @@ -16,8 +17,8 @@ import net.sourceforge.pmd.util.datasource.DataSource; * quite large in some scenarios. Consider using * {@link AbstractIncrementingRenderer} which can use significantly less memory. * - * Subclasses should implement the {@link #end()} method to output the - * {@link #report}. + *

Subclasses should only implement the {@link #end()} method to output the + * complete {@link #report}. * * @see AbstractIncrementingRenderer */ @@ -42,7 +43,15 @@ public abstract class AbstractAccumulatingRenderer extends AbstractRenderer { // does nothing - override if necessary } + /** + * {@inheritDoc} + * + * @deprecated This is internal API. Do not override when extending {@link AbstractAccumulatingRenderer}. + * In PMD7 this method will be made final. + */ @Override + @InternalApi + @Deprecated public void renderFileReport(Report report) throws IOException { this.report.merge(report); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/ExcludeLinesTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/ExcludeLinesTest.java index 2d68f04ba7..a1f3e4bd9f 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/ExcludeLinesTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/ExcludeLinesTest.java @@ -34,15 +34,17 @@ public class ExcludeLinesTest extends RuleTst { @Test public void testAlternateMarker() throws Exception { - PMD p = new PMD(); - p.getConfiguration().setSuppressMarker("FOOBAR"); + PMDConfiguration configuration = new PMDConfiguration(); + configuration.setSuppressMarker("FOOBAR"); RuleContext ctx = new RuleContext(); Report r = new Report(); ctx.setReport(r); ctx.setSourceCodeFile(new File("n/a")); ctx.setLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion()); RuleSet rules = RuleSet.forSingleRule(rule); - p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx); + + SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(configuration); + sourceCodeProcessor.processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx); assertTrue(r.isEmpty()); assertEquals(r.getSuppressedRuleViolations().size(), 1); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/metrics/xpath/XPathMetricFunctionTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/metrics/xpath/XPathMetricFunctionTest.java index 1114a434ee..0ffe21da2e 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/metrics/xpath/XPathMetricFunctionTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/metrics/xpath/XPathMetricFunctionTest.java @@ -13,7 +13,7 @@ import java.util.Iterator; import org.junit.Test; import org.junit.rules.ExpectedException; -import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.PMDException; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.Rule; @@ -21,6 +21,7 @@ import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSets; import net.sourceforge.pmd.RuleViolation; +import net.sourceforge.pmd.SourceCodeProcessor; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.java.JavaLanguageModule; import net.sourceforge.pmd.lang.java.xpath.MetricFunction; @@ -50,14 +51,14 @@ public class XPathMetricFunctionTest { private Iterator getViolations(Rule rule, String code) throws PMDException { - PMD p = new PMD(); RuleContext ctx = new RuleContext(); Report report = new Report(); ctx.setReport(report); ctx.setSourceCodeFile(new File("n/a")); ctx.setIgnoreExceptions(false); // for test, we want immediate exceptions thrown and not collect them RuleSet rules = RuleSet.forSingleRule(rule); - p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx); + SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(new PMDConfiguration()); + sourceCodeProcessor.processSourceCode(new StringReader(code), new RuleSets(rules), ctx); return report.iterator(); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java index e563974933..464bc03c96 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java @@ -15,6 +15,7 @@ import java.util.List; import org.junit.Test; import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.PMDException; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.Rule; @@ -22,6 +23,7 @@ import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSets; import net.sourceforge.pmd.RuleViolation; +import net.sourceforge.pmd.SourceCodeProcessor; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.lang.Parser; @@ -200,13 +202,13 @@ public class XPathRuleTest extends RuleTst { } private static Report getReportForTestString(Rule r, String test) throws PMDException { - PMD p = new PMD(); RuleContext ctx = new RuleContext(); Report report = new Report(); ctx.setReport(report); ctx.setSourceCodeFile(new File("n/a")); RuleSet rules = RuleSet.forSingleRule(r); - p.getSourceCodeProcessor().processSourceCode(new StringReader(test), new RuleSets(rules), ctx); + SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(new PMDConfiguration()); + sourceCodeProcessor.processSourceCode(new StringReader(test), new RuleSets(rules), ctx); return report; } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java index 1207929f00..d5714dcce9 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java @@ -34,7 +34,7 @@ import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; -import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.PMDException; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.Rule; @@ -44,6 +44,7 @@ import net.sourceforge.pmd.RuleSetNotFoundException; import net.sourceforge.pmd.RuleSets; import net.sourceforge.pmd.RuleViolation; import net.sourceforge.pmd.RulesetsFactoryUtils; +import net.sourceforge.pmd.SourceCodeProcessor; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.properties.PropertyDescriptor; @@ -268,18 +269,18 @@ public abstract class RuleTst { public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion, boolean isUseAuxClasspath) { try { - PMD p = new PMD(); - p.getConfiguration().setDefaultLanguageVersion(languageVersion); - p.getConfiguration().setIgnoreIncrementalAnalysis(true); + PMDConfiguration configuration = new PMDConfiguration(); + configuration.setDefaultLanguageVersion(languageVersion); + configuration.setIgnoreIncrementalAnalysis(true); if (isUseAuxClasspath) { // configure the "auxclasspath" option for unit testing - p.getConfiguration().prependClasspath("."); + configuration.prependClasspath("."); } else { // simple class loader, that doesn't delegate to parent. // this allows us in the tests to simulate PMD run without // auxclasspath, not even the classes from the test dependencies // will be found. - p.getConfiguration().setClassLoader(new ClassLoader() { + configuration.setClassLoader(new ClassLoader() { @Override protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { if (name.startsWith("java.") || name.startsWith("javax.")) { @@ -295,7 +296,8 @@ public abstract class RuleTst { ctx.setLanguageVersion(languageVersion); ctx.setIgnoreExceptions(false); RuleSet rules = RuleSet.forSingleRule(rule); - p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx); + SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(configuration); + sourceCodeProcessor.processSourceCode(new StringReader(code), new RuleSets(rules), ctx); } catch (Exception e) { throw new RuntimeException(e); }