-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Deprecate some syntax for ruleset references #3958
Merged
adangel
merged 10 commits into
pmd:master
from
oowekyala:issue2352-ruleset-deprecations
May 28, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db9a1d9
Deprecate some syntax about ruleset references
oowekyala 0542b33
Add tests and fix logic
oowekyala b0fd5c4
Update release notes
oowekyala abf7d71
Remove doc for deprecated things
oowekyala f27ea90
Fix test
oowekyala 9f98d5e
Merge branch 'master' into issue2352-ruleset-deprecations
oowekyala fa41583
Fix tests
oowekyala 380d81c
Add todo comment
oowekyala 425015c
Merge branch 'master' into pr/3958
adangel 650e66b
[doc] Update release notes (#2352, #3958)
adangel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
|
@@ -58,16 +59,6 @@ | |
* <td>all</td> | ||
* </tr> | ||
* <tr> | ||
* <td>java-basic</td> | ||
* <td>rulesets/java/basic.xml</td> | ||
* <td>all</td> | ||
* </tr> | ||
* <tr> | ||
* <td>50</td> | ||
* <td>rulesets/releases/50.xml</td> | ||
* <td>all</td> | ||
* </tr> | ||
* <tr> | ||
* <td>rulesets/java/basic.xml/EmptyCatchBlock</td> | ||
* <td>rulesets/java/basic.xml</td> | ||
* <td>EmptyCatchBlock</td> | ||
|
@@ -85,12 +76,21 @@ | |
@Deprecated | ||
@InternalApi | ||
public class RuleSetReferenceId { | ||
|
||
// todo this class has issues... What is even an "external" ruleset? | ||
// terminology and API should be clarified. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep in mind at some point we marked it as InternalApi. |
||
|
||
// use the logger of RuleSetFactory, because the warnings conceptually come from there. | ||
private static final Logger LOG = Logger.getLogger(RuleSetFactory.class.getName()); | ||
|
||
private final boolean external; | ||
private final String ruleSetFileName; | ||
private final boolean allRules; | ||
private final String ruleName; | ||
private final RuleSetReferenceId externalRuleSetReferenceId; | ||
|
||
private final String originalRef; | ||
|
||
/** | ||
* Construct a RuleSetReferenceId for the given single ID string. | ||
* | ||
|
@@ -110,19 +110,34 @@ public RuleSetReferenceId(final String id) { | |
* Rule. The external RuleSetReferenceId will be responsible for producing | ||
* the InputStream containing the Rule. | ||
* | ||
* @param id | ||
* The id string. | ||
* @param externalRuleSetReferenceId | ||
* A RuleSetReferenceId to associate with this new instance. | ||
* @throws IllegalArgumentException | ||
* If the ID contains a comma character. | ||
* @throws IllegalArgumentException | ||
* If external RuleSetReferenceId is not external. | ||
* @throws IllegalArgumentException | ||
* If the ID is not Rule reference when there is an external | ||
* RuleSetReferenceId. | ||
* @param id The id string. | ||
* @param externalRuleSetReferenceId A RuleSetReferenceId to associate with this new instance. | ||
* | ||
* @throws IllegalArgumentException If the ID contains a comma character. | ||
* @throws IllegalArgumentException If external RuleSetReferenceId is not external. | ||
* @throws IllegalArgumentException If the ID is not Rule reference when there is an external | ||
* RuleSetReferenceId. | ||
*/ | ||
public RuleSetReferenceId(final String id, final RuleSetReferenceId externalRuleSetReferenceId) { | ||
this(id, externalRuleSetReferenceId, false); | ||
} | ||
|
||
/** | ||
* Construct a RuleSetReferenceId for the given single ID string. If an | ||
* external RuleSetReferenceId is given, the ID must refer to a non-external | ||
* Rule. The external RuleSetReferenceId will be responsible for producing | ||
* the InputStream containing the Rule. | ||
* | ||
* @param id The id string. | ||
* @param externalRuleSetReferenceId A RuleSetReferenceId to associate with this new instance. | ||
* | ||
* @throws IllegalArgumentException If the ID contains a comma character. | ||
* @throws IllegalArgumentException If external RuleSetReferenceId is not external. | ||
* @throws IllegalArgumentException If the ID is not Rule reference when there is an external | ||
* RuleSetReferenceId. | ||
*/ | ||
RuleSetReferenceId(final String id, final RuleSetReferenceId externalRuleSetReferenceId, boolean warnDeprecated) { | ||
this.originalRef = id; | ||
|
||
if (externalRuleSetReferenceId != null && !externalRuleSetReferenceId.isExternal()) { | ||
throw new IllegalArgumentException("Cannot pair with non-external <" + externalRuleSetReferenceId + ">."); | ||
|
@@ -177,8 +192,16 @@ public RuleSetReferenceId(final String id, final RuleSetReferenceId externalRule | |
allRules = tempRuleName == null; | ||
} else { | ||
// resolve the ruleset name - it's maybe a built in ruleset | ||
String builtinRuleSet = resolveBuiltInRuleset(tempRuleSetFileName); | ||
String expandedRuleset = resolveDeprecatedBuiltInRulesetShorthand(tempRuleSetFileName); | ||
String builtinRuleSet = expandedRuleset == null ? tempRuleSetFileName : expandedRuleset; | ||
if (checkRulesetExists(builtinRuleSet)) { | ||
if (expandedRuleset != null && warnDeprecated) { | ||
LOG.warning( | ||
"Ruleset reference '" + tempRuleSetFileName + "' uses a deprecated form, use '" | ||
+ builtinRuleSet + "' instead" | ||
Comment on lines
+200
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
); | ||
} | ||
|
||
external = true; | ||
ruleSetFileName = builtinRuleSet; | ||
ruleName = tempRuleName; | ||
|
@@ -249,25 +272,22 @@ private boolean checkRulesetExists(final String name) { | |
* the ruleset name | ||
* @return the full classpath to the ruleset | ||
*/ | ||
private String resolveBuiltInRuleset(final String name) { | ||
String result = null; | ||
if (name != null) { | ||
// Likely a simple RuleSet name | ||
int index = name.indexOf('-'); | ||
if (index >= 0) { | ||
// Standard short name | ||
result = "rulesets/" + name.substring(0, index) + '/' + name.substring(index + 1) + ".xml"; | ||
} else { | ||
// A release RuleSet? | ||
if (name.matches("[0-9]+.*")) { | ||
result = "rulesets/releases/" + name + ".xml"; | ||
} else { | ||
// Appears to be a non-standard RuleSet name | ||
result = name; | ||
} | ||
} | ||
private String resolveDeprecatedBuiltInRulesetShorthand(final String name) { | ||
if (name == null) { | ||
return null; | ||
} | ||
return result; | ||
// Likely a simple RuleSet name | ||
int index = name.indexOf('-'); | ||
if (index > 0) { | ||
// Standard short name | ||
return "rulesets/" + name.substring(0, index) + '/' + name.substring(index + 1) + ".xml"; | ||
} | ||
// A release RuleSet? | ||
if (name.matches("[0-9]+.*")) { | ||
return "rulesets/releases/" + name + ".xml"; | ||
} | ||
// Appears to be a non-standard RuleSet name | ||
return null; | ||
} | ||
|
||
/** | ||
|
@@ -330,19 +350,31 @@ private static boolean isFullRuleSetName(String name) { | |
* Parse a String comma separated list of RuleSet reference IDs into a List | ||
* of RuleReferenceId instances. | ||
* | ||
* @param referenceString | ||
* A comma separated list of RuleSet reference IDs. | ||
* @param referenceString A comma separated list of RuleSet reference IDs. | ||
* | ||
* @return The corresponding List of RuleSetReferenceId instances. | ||
*/ | ||
public static List<RuleSetReferenceId> parse(String referenceString) { | ||
return parse(referenceString, false); | ||
} | ||
|
||
/** | ||
* Parse a String comma separated list of RuleSet reference IDs into a List | ||
* of RuleReferenceId instances. | ||
* | ||
* @param referenceString A comma separated list of RuleSet reference IDs. | ||
* | ||
* @return The corresponding List of RuleSetReferenceId instances. | ||
*/ | ||
public static List<RuleSetReferenceId> parse(String referenceString, boolean warnDeprecated) { | ||
List<RuleSetReferenceId> references = new ArrayList<>(); | ||
if (referenceString != null && referenceString.trim().length() > 0) { | ||
|
||
if (referenceString.indexOf(',') == -1) { | ||
references.add(new RuleSetReferenceId(referenceString)); | ||
references.add(new RuleSetReferenceId(referenceString, null, warnDeprecated)); | ||
} else { | ||
for (String name : referenceString.split(",")) { | ||
references.add(new RuleSetReferenceId(name.trim())); | ||
references.add(new RuleSetReferenceId(name.trim(), null, warnDeprecated)); | ||
} | ||
} | ||
} | ||
|
@@ -405,9 +437,9 @@ public InputStream getInputStream(final ResourceLoader rl) throws RuleSetNotFoun | |
InputStream in = StringUtils.isBlank(ruleSetFileName) ? null | ||
: rl.loadResourceAsStream(ruleSetFileName); | ||
if (in == null) { | ||
throw new RuleSetNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName | ||
throw new RuleSetNotFoundException("Cannot resolve rule/ruleset reference '" + originalRef | ||
+ "'" + ". Make sure the resource is a valid file or URL and is on the CLASSPATH. " | ||
+ "Here's the current classpath: " + System.getProperty("java.class.path")); | ||
+ "Use --debug (or a fine log level) to see the current classpath."); | ||
} | ||
return in; | ||
} else { | ||
|
@@ -422,8 +454,11 @@ public InputStream getInputStream(final ResourceLoader rl) throws RuleSetNotFoun | |
* <i>ruleSetFileName</i> for all Rule external references, | ||
* <i>ruleSetFileName/ruleName</i>, for a single Rule external | ||
* references, or <i>ruleName</i> otherwise. | ||
* | ||
* @deprecated Do not rely on the format of this method, it may be changed in PMD 7. | ||
*/ | ||
@Override | ||
@Deprecated | ||
public String toString() { | ||
if (ruleSetFileName != null) { | ||
if (allRules) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... here we silently used only the first reference :)