Use list of ruleset in configuration
This commit is contained in:
1 parent
789e4aa730
commit
86ea3a0fb7
5 files changed
+44
-27
No files matched your search
@@ -220,11 +220,7 @@ public class PMD {
|
||||
|
||||
// Load the RuleSets
|
||||
final RuleSetLoader ruleSetFactory = RuleSetLoader.fromPmdConfig(configuration);
|
||||
List<String> rulesetPaths = Arrays.asList(configuration.getRuleSets().split(","));
|
||||
final RuleSets ruleSets = new RuleSets(getRuleSetsWithBenchmark(rulesetPaths, ruleSetFactory));
|
||||
if (ruleSets == null) {
|
||||
return PMDCommandLineInterface.NO_ERRORS_STATUS;
|
||||
}
|
||||
final RuleSets ruleSets = new RuleSets(getRuleSetsWithBenchmark(configuration.getRuleSetPaths(), ruleSetFactory));
|
||||
|
||||
final Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
|
||||
final List<DataSource> files = getApplicableFiles(configuration, languages);
|
||||
@@ -273,7 +269,7 @@ public class PMD {
|
||||
|
||||
private static List<RuleSet> getRuleSetsWithBenchmark(List<String> rulesetPaths, RuleSetLoader factory) {
|
||||
try (TimedOperation to = TimeTracker.startOperation(TimedOperationCategory.LOAD_RULES)) {
|
||||
List<RuleSet> ruleSets = null;
|
||||
List<RuleSet> ruleSets;
|
||||
try {
|
||||
ruleSets = factory.loadFromResources(rulesetPaths);
|
||||
printRuleNamesInDebug(ruleSets);
|
||||
|
||||
@@ -6,10 +6,14 @@ package net.sourceforge.pmd;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
|
||||
import net.sourceforge.pmd.cache.AnalysisCache;
|
||||
import net.sourceforge.pmd.cache.FileAnalysisCache;
|
||||
import net.sourceforge.pmd.cache.NoopAnalysisCache;
|
||||
@@ -44,7 +48,7 @@ import net.sourceforge.pmd.util.ClasspathClassLoader;
|
||||
*
|
||||
* <p>The aspects related to Rules and Source files are:</p>
|
||||
* <ul>
|
||||
* <li>A comma separated list of RuleSets URIs. {@link #getRuleSets()}</li>
|
||||
* <li>RuleSets URIs: {@link #getRuleSetPaths()}</li>
|
||||
* <li>A minimum priority threshold when loading Rules from RuleSets, defaults
|
||||
* to {@link RulePriority#LOW}. {@link #getMinimumPriority()}</li>
|
||||
* <li>The character encoding of source files, defaults to the system default as
|
||||
@@ -86,7 +90,7 @@ public class PMDConfiguration extends AbstractConfiguration {
|
||||
private LanguageVersionDiscoverer languageVersionDiscoverer = new LanguageVersionDiscoverer();
|
||||
|
||||
// Rule and source file options
|
||||
private String ruleSets;
|
||||
private List<String> ruleSets;
|
||||
private RulePriority minimumPriority = RulePriority.LOW;
|
||||
private String inputPaths;
|
||||
private String inputUri;
|
||||
@@ -258,19 +262,44 @@ public class PMDConfiguration extends AbstractConfiguration {
|
||||
* Get the comma separated list of RuleSet URIs.
|
||||
*
|
||||
* @return The RuleSet URIs.
|
||||
*
|
||||
* @deprecated Use {@link #getRuleSetPaths()}
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecatedUntil700
|
||||
public String getRuleSets() {
|
||||
return String.join(",", ruleSets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of ruleset URIs.
|
||||
*
|
||||
* @see RuleSetLoader#loadFromResource(String)
|
||||
*/
|
||||
public List<String> getRuleSetPaths() {
|
||||
return ruleSets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the rulesets.
|
||||
*
|
||||
* @throws NullPointerException If the parameter is null
|
||||
*/
|
||||
public void setRuleSets(@NonNull List<String> ruleSets) {
|
||||
this.ruleSets = new ArrayList<>(ruleSets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the comma separated list of RuleSet URIs.
|
||||
*
|
||||
* @param ruleSets
|
||||
* the rulesets to set
|
||||
* @param ruleSets the rulesets to set
|
||||
*
|
||||
* @deprecated Use {@link #setRuleSets(List)}
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecatedUntil700
|
||||
public void setRuleSets(String ruleSets) {
|
||||
this.ruleSets = ruleSets;
|
||||
this.ruleSets = Arrays.asList(ruleSets.split(","));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,6 +51,7 @@ public class PMDTaskImpl {
|
||||
private final List<Formatter> formatters = new ArrayList<>();
|
||||
private final List<FileSet> filesets = new ArrayList<>();
|
||||
private final PMDConfiguration configuration = new PMDConfiguration();
|
||||
private final String rulesetPaths;
|
||||
private boolean failOnError;
|
||||
private boolean failOnRuleViolation;
|
||||
private int maxRuleViolations = 0;
|
||||
@@ -68,7 +69,7 @@ public class PMDTaskImpl {
|
||||
if (this.maxRuleViolations > 0) {
|
||||
this.failOnRuleViolation = true;
|
||||
}
|
||||
configuration.setRuleSets(task.getRulesetFiles());
|
||||
this.rulesetPaths = task.getRulesetFiles() == null ? "" : task.getRulesetFiles();
|
||||
configuration.setRuleSetFactoryCompatibilityEnabled(!task.isNoRuleSetCompatibility());
|
||||
if (task.getEncoding() != null) {
|
||||
configuration.setSourceEncoding(task.getEncoding());
|
||||
@@ -106,13 +107,10 @@ public class PMDTaskImpl {
|
||||
.loadResourcesWith(setupResourceLoader());
|
||||
|
||||
// This is just used to validate and display rules. Each thread will create its own ruleset
|
||||
String ruleSetString = configuration.getRuleSets();
|
||||
if (StringUtils.isNotBlank(ruleSetString)) {
|
||||
// Substitute env variables/properties
|
||||
configuration.setRuleSets(project.replaceProperties(ruleSetString));
|
||||
}
|
||||
// Substitute env variables/properties
|
||||
String ruleSetString = project.replaceProperties(rulesetPaths);
|
||||
|
||||
List<String> rulesets = Arrays.asList(configuration.getRuleSets().split(","));
|
||||
List<String> rulesets = Arrays.asList(ruleSetString.split(","));
|
||||
List<RuleSet> rulesetList = rulesetLoader.loadFromResources(rulesets);
|
||||
if (rulesetList.isEmpty()) {
|
||||
throw new BuildException("No rulesets");
|
||||
@@ -296,7 +294,7 @@ public class PMDTaskImpl {
|
||||
}
|
||||
|
||||
private void logRulesUsed(List<RuleSet> rulesets) {
|
||||
project.log("Using these rulesets: " + configuration.getRuleSets(), Project.MSG_VERBOSE);
|
||||
project.log("Using these rulesets: " + rulesetPaths, Project.MSG_VERBOSE);
|
||||
|
||||
for (RuleSet ruleSet : rulesets) {
|
||||
for (Rule rule : ruleSet.getRules()) {
|
||||
|
||||
@@ -6,6 +6,7 @@ package net.sourceforge.pmd.cli;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -203,7 +204,7 @@ public class PMDParameters {
|
||||
configuration.setReportFile(this.getReportfile());
|
||||
configuration.setReportProperties(this.getProperties());
|
||||
configuration.setReportShortNames(this.isShortnames());
|
||||
configuration.setRuleSets(this.getRulesets());
|
||||
configuration.setRuleSets(Arrays.asList(this.getRulesets().split(",")));
|
||||
configuration.setRuleSetFactoryCompatibilityEnabled(!this.noRuleSetCompatibility);
|
||||
configuration.setShowSuppressedViolations(this.isShowsuppressed());
|
||||
configuration.setSourceEncoding(this.getEncoding());
|
||||
|
||||
@@ -111,13 +111,6 @@ public class ConfigurationTest {
|
||||
Assert.assertArrayEquals(expectedUris, uris);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleSets() {
|
||||
PMDConfiguration configuration = new PMDConfiguration();
|
||||
assertEquals("Default RuleSets", null, configuration.getRuleSets());
|
||||
configuration.setRuleSets("/rulesets/basic.xml");
|
||||
assertEquals("Changed RuleSets", "/rulesets/basic.xml", configuration.getRuleSets());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinimumPriority() {
|
||||
|
||||
Reference in new issue
Block a user