From 94028a1ce6289ea794f109308eef672f072215c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 12 Dec 2020 17:38:40 +0100 Subject: [PATCH] Remove deprecated ctors of RuleSetFactory --- .../net/sourceforge/pmd/RuleSetFactory.java | 48 +------ .../net/sourceforge/pmd/RuleSetLoader.java | 12 +- .../sourceforge/pmd/RulesetsFactoryUtils.java | 130 +----------------- .../sourceforge/pmd/RuleSetFactoryTest.java | 6 +- .../pmd/internal/StageDependencyTest.java | 5 +- .../pmd/docs/RuleDocGeneratorTest.java | 11 +- .../pmd/lang/ast/test/BaseParsingHelper.kt | 2 +- 7 files changed, 22 insertions(+), 192 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index aa010c7c60..bd3069ef67 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -62,58 +62,14 @@ public class RuleSetFactory { private final Map parsedRulesets = new HashMap<>(); - /** - * @deprecated Use a {@link RuleSetLoader} to build a new factory - */ - @Deprecated // to be removed with PMD 7.0.0. - public RuleSetFactory() { - this(new ResourceLoader(), RulePriority.LOW, false, true); - } - - /** - * @deprecated Use a {@link RuleSetLoader} to build a new factory - */ - @Deprecated // to be removed with PMD 7.0.0. - public RuleSetFactory(final ClassLoader classLoader, final RulePriority minimumPriority, - final boolean warnDeprecated, final boolean enableCompatibility) { - this(new ResourceLoader(classLoader), minimumPriority, warnDeprecated, enableCompatibility); - } - - /** - * @deprecated Use a {@link RuleSetLoader} to build a new factory - */ - @Deprecated // to be hidden with PMD 7.0.0. - public RuleSetFactory(final ResourceLoader resourceLoader, final RulePriority minimumPriority, - final boolean warnDeprecated, final boolean enableCompatibility) { - this(resourceLoader, minimumPriority, warnDeprecated, enableCompatibility, false); - } - RuleSetFactory(final ResourceLoader resourceLoader, final RulePriority minimumPriority, - final boolean warnDeprecated, final boolean enableCompatibility, boolean includeDeprecatedRuleReferences) { + final boolean warnDeprecated, final RuleSetFactoryCompatibility compatFilter, boolean includeDeprecatedRuleReferences) { this.resourceLoader = resourceLoader; this.minimumPriority = minimumPriority; this.warnDeprecated = warnDeprecated; this.includeDeprecatedRuleReferences = includeDeprecatedRuleReferences; - if (enableCompatibility) { - this.compatibilityFilter = new RuleSetFactoryCompatibility(); - } else { - this.compatibilityFilter = null; - } - } - - /** - * Constructor copying all configuration from another factory. - * - * @param factory The factory whose configuration to copy. - * @param warnDeprecated Whether deprecation warnings are to be produced by this - * factory - * - * @deprecated Use {@link #toLoader()} to rebuild a factory from a configuration - */ - @Deprecated - public RuleSetFactory(final RuleSetFactory factory, final boolean warnDeprecated) { - this(factory.resourceLoader, factory.minimumPriority, warnDeprecated, factory.compatibilityFilter != null); + this.compatibilityFilter = compatFilter; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetLoader.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetLoader.java index 36ba54503d..3d5a0173c8 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetLoader.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetLoader.java @@ -12,6 +12,8 @@ import java.util.List; import java.util.Properties; import java.util.logging.Logger; +import org.checkerframework.checker.nullness.qual.Nullable; + import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.util.CollectionUtil; @@ -30,7 +32,7 @@ public final class RuleSetLoader { private ResourceLoader resourceLoader = new ResourceLoader(RuleSetLoader.class.getClassLoader()); private RulePriority minimumPriority = RulePriority.LOW; private boolean warnDeprecated = true; - private boolean enableCompatibility = true; + private @Nullable RuleSetFactoryCompatibility compatFilter; private boolean includeDeprecatedRuleReferences = false; /** @@ -81,7 +83,11 @@ public final class RuleSetLoader { * @return This instance, modified */ public RuleSetLoader enableCompatibility(boolean enable) { - this.enableCompatibility = enable; + if (enable) { + this.compatFilter = RuleSetFactoryCompatibility.INSTANCE; + } else { + this.compatFilter = null; + } return this; } @@ -110,7 +116,7 @@ public final class RuleSetLoader { this.resourceLoader, this.minimumPriority, this.warnDeprecated, - this.enableCompatibility, + this.compatFilter, this.includeDeprecatedRuleReferences ); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RulesetsFactoryUtils.java b/pmd-core/src/main/java/net/sourceforge/pmd/RulesetsFactoryUtils.java index 135736f8a6..cc2b70bf56 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RulesetsFactoryUtils.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RulesetsFactoryUtils.java @@ -11,7 +11,6 @@ import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.benchmark.TimeTracker; import net.sourceforge.pmd.benchmark.TimedOperation; import net.sourceforge.pmd.benchmark.TimedOperationCategory; -import net.sourceforge.pmd.util.ResourceLoader; /** * @deprecated Use a {@link RuleSetLoader} instead @@ -79,35 +78,6 @@ public final class RulesetsFactoryUtils { } } - /** - * @deprecated Use a {@link RuleSetLoader} - */ - @InternalApi - @Deprecated - public static RuleSetFactory getRulesetFactory(final PMDConfiguration configuration, - final ResourceLoader resourceLoader) { - return new RuleSetFactory(resourceLoader, configuration.getMinimumPriority(), true, - configuration.isRuleSetFactoryCompatibilityEnabled()); - } - - /** - * Returns a ruleset factory which uses the classloader for PMD - * classes to resolve resource references. - * - * @param configuration PMD configuration, contains info about the - * factory parameters - * - * @return A ruleset factory - * - * @see #createFactory(PMDConfiguration, ClassLoader) - * - * @deprecated Use {@link RuleSetLoader#fromPmdConfig(PMDConfiguration)} - */ - @Deprecated - public static RuleSetFactory createFactory(final PMDConfiguration configuration) { - return createFactory(configuration, RulesetsFactoryUtils.class.getClassLoader()); - } - /** * Returns a ruleset factory with default parameters. It doesn't prune * rules based on priority, and doesn't warn for deprecations. @@ -116,105 +86,9 @@ public final class RulesetsFactoryUtils { * * @see RuleSetLoader */ + @Deprecated public static RuleSetFactory defaultFactory() { - return new RuleSetFactory(); - } - - /** - * Returns a ruleset factory which uses the provided {@link ClassLoader} - * to resolve resource references. It warns for deprecated rule usages. - * - * @param configuration PMD configuration, contains info about the - * factory parameters - * @param classLoader Class loader to load resources - * - * @return A ruleset factory - * - * @see #createFactory(PMDConfiguration) - * - * @deprecated Use a {@link RuleSetLoader} - */ - @Deprecated - public static RuleSetFactory createFactory(final PMDConfiguration configuration, ClassLoader classLoader) { - return createFactory(classLoader, - configuration.getMinimumPriority(), - true, - configuration.isRuleSetFactoryCompatibilityEnabled()); - } - - /** - * Returns a ruleset factory which uses the provided {@link ClassLoader} - * to resolve resource references. - * - * @param minimumPriority Minimum priority for rules to be included - * @param warnDeprecated If true, print warnings when deprecated rules are included - * @param enableCompatibility If true, rule references to moved rules are mapped to their - * new location if they are known - * @param classLoader Class loader to load resources - * - * @return A ruleset factory - * - * @see #createFactory(PMDConfiguration) - * - * @deprecated Use a {@link RuleSetLoader} - */ - @Deprecated - public static RuleSetFactory createFactory(ClassLoader classLoader, - RulePriority minimumPriority, - boolean warnDeprecated, - boolean enableCompatibility) { - - return new RuleSetFactory(new ResourceLoader(classLoader), minimumPriority, warnDeprecated, enableCompatibility); - } - - /** - * Returns a ruleset factory which uses the classloader for PMD - * classes to resolve resource references. - * - * @param minimumPriority Minimum priority for rules to be included - * @param warnDeprecated If true, print warnings when deprecated rules are included - * @param enableCompatibility If true, rule references to moved rules are mapped to their - * new location if they are known - * - * @return A ruleset factory - * - * @see #createFactory(PMDConfiguration) - * - * @deprecated Use a {@link RuleSetLoader} - */ - @Deprecated - public static RuleSetFactory createFactory(RulePriority minimumPriority, - boolean warnDeprecated, - boolean enableCompatibility) { - return new RuleSetFactory(new ResourceLoader(), minimumPriority, warnDeprecated, enableCompatibility); - } - - /** - * Returns a ruleset factory which uses the classloader for PMD - * classes to resolve resource references. - * - * @param minimumPriority Minimum priority for rules to be included - * @param warnDeprecated If true, print warnings when deprecated rules are included - * @param enableCompatibility If true, rule references to moved rules are mapped to their - * new location if they are known - * @param includeDeprecatedRuleReferences If true, deprecated rule references are retained. Usually, these - * references are ignored, since they indicate renamed/moved rules, and the referenced - * rule is often included in the same ruleset. Enabling this might result in - * duplicated rules. - * - * @return A ruleset factory - * - * @see #createFactory(PMDConfiguration) - * @deprecated Use a {@link RuleSetLoader} - */ - @Deprecated - public static RuleSetFactory createFactory(RulePriority minimumPriority, - boolean warnDeprecated, - boolean enableCompatibility, - boolean includeDeprecatedRuleReferences) { - - return new RuleSetFactory(new ResourceLoader(), minimumPriority, warnDeprecated, enableCompatibility, - includeDeprecatedRuleReferences); + return new RuleSetLoader().toFactory(); } /** diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java index 52e52f4ff5..1abaa6bc43 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java @@ -254,12 +254,10 @@ public class RuleSetFactoryTest { *

When loading this ruleset at a whole for generating the documentation, we should still * include the deprecated rule reference, so that we can create a nice documentation. * - * @throws Exception */ @Test - public void testRuleSetWithDeprecatedRenamedRuleForDoc() throws Exception { - RuleSetFactory rsf = RulesetsFactoryUtils.createFactory(RulePriority.LOW, false, false, true); - RuleSet rs = rsf.createRuleSet(createRuleSetReferenceId("\n" + "\n" + public void testRuleSetWithDeprecatedRenamedRuleForDoc() { + RuleSet rs = new RuleSetLoader().loadFromResource(createRuleSetReferenceId("\n" + "\n" + " ruleset desc\n" + " " + " " diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/internal/StageDependencyTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/internal/StageDependencyTest.java index aa8760c33b..5eb462cd5a 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/internal/StageDependencyTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/internal/StageDependencyTest.java @@ -18,7 +18,6 @@ import net.sourceforge.pmd.PMDException; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RuleSet; -import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSets; import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguageRegistry; @@ -142,9 +141,9 @@ public class StageDependencyTest { private static RuleSets withRules(Rule r, Rule... rs) { List rsets = new ArrayList<>(); - rsets.add(new RuleSetFactory().createSingleRuleRuleSet(r)); + rsets.add(RuleSet.forSingleRule(r)); for (Rule rule : rs) { - rsets.add(new RuleSetFactory().createSingleRuleRuleSet(rule)); + rsets.add(RuleSet.forSingleRule(rule)); } return new RuleSets(rsets); diff --git a/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java b/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java index 23d91a043d..7d087032ae 100644 --- a/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java +++ b/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java @@ -21,11 +21,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import net.sourceforge.pmd.RulePriority; import net.sourceforge.pmd.RuleSet; -import net.sourceforge.pmd.RuleSetFactory; -import net.sourceforge.pmd.RuleSetNotFoundException; -import net.sourceforge.pmd.RulesetsFactoryUtils; +import net.sourceforge.pmd.RuleSetLoader; import net.sourceforge.pmd.docs.MockedFileWriter.FileEntry; public class RuleDocGeneratorTest { @@ -59,11 +56,11 @@ public class RuleDocGeneratorTest { } @Test - public void testSingleRuleset() throws RuleSetNotFoundException, IOException { + public void testSingleRuleset() throws IOException { RuleDocGenerator generator = new RuleDocGenerator(writer, root); - RuleSetFactory rsf = RulesetsFactoryUtils.createFactory(RulePriority.LOW, false, false, true); - RuleSet ruleset = rsf.createRuleSet("rulesets/ruledoctest/sample.xml"); + RuleSetLoader rsf = new RuleSetLoader(); + RuleSet ruleset = rsf.loadFromResource("rulesets/ruledoctest/sample.xml"); generator.generate(Arrays.asList(ruleset), Arrays.asList( 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 418e27aaee..6b5b6aaa91 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 @@ -217,7 +217,7 @@ abstract class BaseParsingHelper, T : RootNode ctx.report = report ctx.sourceCodeFile = File(filename) ctx.isIgnoreExceptions = false - val rules = RuleSetFactory().createSingleRuleRuleSet(rule) + val rules = RuleSet.forSingleRule(rule) try { p.sourceCodeProcessor.processSourceCode(StringReader(code), RuleSets(rules), ctx) } catch (e: PMDException) {