Skip to content

Commit

Permalink
[core] Remove support for deprecated rule set references notation
Browse files Browse the repository at this point in the history
The old notation <lang>-<ruleset> is not supported anymore. It is
now interpreted as a ruleset reference without a ruleset and just
referencing a single rule. Also the release number notation is
not supported anymore.

Since RuleSetReferenceId is Internal+Deprecated, no API changes.
Clarified External/Internal: A RuleSetReference is either absolute
(RuleSet is known) or relative (RuleSet is not known).

Fixes #4313
  • Loading branch information
adangel committed Jan 5, 2024
1 parent a558fd1 commit 7b24edc
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 510 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -105,6 +105,7 @@ in the Migration Guide.
* [#4723](https://github.com/pmd/pmd/issues/4723): \[cli] Launch fails for "bash pmd"
* core
* [#1027](https://github.com/pmd/pmd/issues/1027): \[core] Apply the new PropertyDescriptor&lt;Pattern&gt; type where applicable
* [#4313](https://github.com/pmd/pmd/issues/4313): \[core] Remove support for &lt;lang&gt;-&lt;ruleset&gt; hyphen notation for ruleset references
* [#4674](https://github.com/pmd/pmd/issues/4674): \[core] WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass
* [#4750](https://github.com/pmd/pmd/pull/4750): \[core] Fix flaky SummaryHTMLRenderer
* doc
Expand Down Expand Up @@ -561,6 +562,7 @@ See also [Detailed Release Notes for PMD 7]({{ baseurl }}pmd_release_notes_pmd7.
* [#4204](https://github.com/pmd/pmd/issues/4204): \[core] Provide a CpdAnalysis class as a programmatic entry point into CPD
* [#4301](https://github.com/pmd/pmd/issues/4301): \[core] Remove deprecated property concrete classes
* [#4302](https://github.com/pmd/pmd/issues/4302): \[core] Migrate Property Framework API to Java 8
* [#4313](https://github.com/pmd/pmd/issues/4313): \[core] Remove support for &lt;lang&gt;-&lt;ruleset&gt; hyphen notation for ruleset references
* [#4323](https://github.com/pmd/pmd/issues/4323): \[core] Refactor CPD integration
* [#4353](https://github.com/pmd/pmd/pull/4353): \[core] Micro optimizations for Node API
* [#4365](https://github.com/pmd/pmd/pull/4365): \[core] Improve benchmarking
Expand Down
6 changes: 0 additions & 6 deletions pmd-cli/src/test/java/net/sourceforge/pmd/cli/PmdCliTest.java
Expand Up @@ -209,12 +209,6 @@ void defaultLogging() throws Exception {
result.checkStdErr(not(containsPattern("Adding file .*"))); // not in debug mode
}

@Test
void testDeprecatedRulesetSyntaxOnCommandLine() throws Exception {
CliExecutionResult result = runCli(VIOLATIONS_FOUND, "--dir", srcDir.toString(), "--rulesets", "dummy-basic");
result.checkStdErr(containsString("Ruleset reference 'dummy-basic' uses a deprecated form, use 'rulesets/dummy/basic.xml' instead"));
}

@Test
void testReportToStdoutNotClosing() throws Exception {
PrintStream originalOut = System.out;
Expand Down
16 changes: 7 additions & 9 deletions pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java
Expand Up @@ -165,9 +165,9 @@ private Rule createRule(RuleSetReferenceId ruleSetReferenceId, boolean withDepre
private @NonNull RuleSet readDocument(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences) {

try (CheckedInputStream inputStream = new CheckedInputStream(ruleSetReferenceId.getInputStream(resourceLoader), new Adler32())) {
if (!ruleSetReferenceId.isExternal()) {
if (!ruleSetReferenceId.isAbsolute()) {
throw new IllegalArgumentException(
"Cannot parse a RuleSet from a non-external reference: <" + ruleSetReferenceId + ">.");
"Cannot parse a RuleSet from a non-absolute reference: <" + ruleSetReferenceId + ">.");
}

XmlMessageHandler printer = getXmlMessagePrinter();
Expand Down Expand Up @@ -446,10 +446,8 @@ private RuleSetReferenceId parseReferenceAndWarn(String ref,
err.at(xmlPlace).warn("Rule reference references a deleted rule, ignoring");
return null; // deleted rule
}
// only emit a warning if we check for deprecated syntax
MessageReporter subReporter = warnDeprecated ? err.at(xmlPlace) : MessageReporter.quiet();

List<RuleSetReferenceId> references = RuleSetReferenceId.parse(ref, subReporter);
List<RuleSetReferenceId> references = RuleSetReferenceId.parse(ref);
if (references.size() > 1 && warnDeprecated) {
err.at(xmlPlace).warn("Using a comma separated list as a ref attribute is deprecated. "
+ "All references but the first are ignored.");
Expand Down Expand Up @@ -527,13 +525,13 @@ private void parseRuleReferenceNode(RuleSetReferenceId ruleSetReferenceId,
RuleSetFactory ruleSetFactory = toLoader().filterAbovePriority(RulePriority.LOW).warnDeprecated(false).toFactory();

boolean isSameRuleSet = false;
if (!otherRuleSetReferenceId.isExternal()
if (!otherRuleSetReferenceId.isAbsolute()
&& containsRule(ruleSetReferenceId, otherRuleSetReferenceId.getRuleName())) {
otherRuleSetReferenceId = new RuleSetReferenceId(ref, ruleSetReferenceId, err.at(REF.getAttributeNode(ruleNode)));
otherRuleSetReferenceId = new RuleSetReferenceId(ref, ruleSetReferenceId);
isSameRuleSet = true;
} else if (otherRuleSetReferenceId.isExternal()
} else if (otherRuleSetReferenceId.isAbsolute()
&& otherRuleSetReferenceId.getRuleSetFileName().equals(ruleSetReferenceId.getRuleSetFileName())) {
otherRuleSetReferenceId = new RuleSetReferenceId(otherRuleSetReferenceId.getRuleName(), ruleSetReferenceId, err.at(REF.getAttributeNode(ruleNode)));
otherRuleSetReferenceId = new RuleSetReferenceId(otherRuleSetReferenceId.getRuleName(), ruleSetReferenceId);
isSameRuleSet = true;
}
// do not ignore deprecated rule references
Expand Down
32 changes: 20 additions & 12 deletions pmd-core/src/main/java/net/sourceforge/pmd/RuleSetLoader.java
Expand Up @@ -17,7 +17,6 @@

import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -153,10 +152,6 @@ public RuleSetFactory toFactory() {
);
}

private @Nullable MessageReporter filteredReporter() {
return warnDeprecated ? reporter : null;
}

/**
* Parses and returns a ruleset from its location. The location may
* be a file system path, or a resource path (see {@link #loadResourcesWith(ClassLoader)}).
Expand All @@ -166,7 +161,7 @@ public RuleSetFactory toFactory() {
* @throws RuleSetLoadException If any error occurs (eg, invalid syntax, or resource not found)
*/
public RuleSet loadFromResource(String rulesetPath) {
return loadFromResource(new RuleSetReferenceId(rulesetPath, null, filteredReporter()));
return loadFromResource(new RuleSetReferenceId(rulesetPath, null));
}

/**
Expand All @@ -178,12 +173,25 @@ public RuleSet loadFromResource(String rulesetPath) {
* @throws RuleSetLoadException If any error occurs (eg, invalid syntax)
*/
public RuleSet loadFromString(String filename, final String rulesetXmlContent) {
return loadFromResource(new RuleSetReferenceId(filename, null, filteredReporter()) {
@Override
public InputStream getInputStream(ResourceLoader rl) {
return new ByteArrayInputStream(rulesetXmlContent.getBytes(StandardCharsets.UTF_8));
}
});
if (filename == null || filename.isEmpty()) {
throw new IllegalArgumentException("Invalid empty filename");
}

ResourceLoader oldLoader = this.resourceLoader;
try {
loadResourcesWith(new ResourceLoader() {
@Override
public @NonNull InputStream loadResourceAsStream(String name) throws IOException {
if (Objects.equals(name, filename)) {
return new ByteArrayInputStream(rulesetXmlContent.getBytes(StandardCharsets.UTF_8));
}
return oldLoader.loadResourceAsStream(name);
}
});
return loadFromResource(new RuleSetReferenceId(filename, null));
} finally {
loadResourcesWith(oldLoader);
}
}

/**
Expand Down

0 comments on commit 7b24edc

Please sign in to comment.