Compare commits

..

No commits in common. "69e2b8a62eea4c7888f46309c80afb255ae9a563" and "77b7d64852535949b7284161bfa774eb463defff" have entirely different histories.

3 changed files with 85 additions and 86 deletions

3
.idea/misc.xml generated
View file

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<entry_points version="2.0"> <entry_points version="2.0">
@ -32,7 +33,7 @@
<option name="customRuleSets"> <option name="customRuleSets">
<list> <list>
<option value="K:\java\semver\config\pmd.xml" /> <option value="K:\java\semver\config\pmd.xml" />
<option value="$PROJECT_DIR$/config/pmd.xml" /> <option value="$PROJECT_DIR$/../bld-exec/config/pmd.xml" />
</list> </list>
</option> </option>
<option name="skipTestSources" value="false" /> <option name="skipTestSources" value="false" />

View file

@ -46,23 +46,23 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
/** /**
* The input paths (source) list. * The input paths (source) list.
*/ */
final List<Path> inputPaths_ = new ArrayList<>(); final List<Path> inputPaths = new ArrayList<>();
/** /**
* The relative roots paths. * The relative roots paths.
*/ */
final List<Path> relativizeRoots_ = new ArrayList<>(); final List<Path> relativizeRoots = new ArrayList<>();
/** /**
* The rule priority. * The rule priority.
*/ */
final RulePriority rulePriority_ = RulePriority.LOW; final RulePriority rulePriority = RulePriority.LOW;
/** /**
* The rule sets list. * The rule sets list.
*/ */
final List<String> ruleSets_ = new ArrayList<>(); final List<String> ruleSets = new ArrayList<>();
/** /**
* The cache location. * The cache location.
*/ */
Path cache_; Path cache;
/** /**
* The encoding. * The encoding.
*/ */
@ -70,51 +70,51 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
/** /**
* The fail on violation toggle. * The fail on violation toggle.
*/ */
boolean failOnViolation_; boolean failOnViolation;
/** /**
* The forced language. * The forced language.
*/ */
LanguageVersion forcedLanguageVersion_; LanguageVersion forcedLanguageVersion;
/** /**
* The path of the ignore file * The path of the ignore file
*/ */
Path ignoreFile_; Path ignoreFile;
/** /**
* The incremental analysis toggle. * The incremental analysis toggle.
*/ */
boolean incrementalAnalysis_ = true; boolean incrementalAnalysis = true;
/** /**
* The input URI. * The input URI.
*/ */
URI inputUri_; URI inputUri;
/** /**
* The default language version(s). * The default language version(s).
*/ */
List<LanguageVersion> languageVersions_; List<LanguageVersion> languageVersions;
/** /**
* The path to the report page. * The path to the report page.
*/ */
Path reportFile_; Path reportFile;
/** /**
* The report format. * The report format.
*/ */
String reportFormat_ = "text"; String reportFormat = "text";
/** /**
* The show suppressed flag. * The show suppressed flag.
*/ */
boolean showSuppressed_; boolean showSuppressed;
/** /**
* THe suppressed marker. * THe suppressed marker.
*/ */
String suppressedMarker_ = "NOPMD"; String suppressedMarker = "NOPMD";
/** /**
* The number of threads. * The number of threads.
*/ */
int threads_ = 1; int threads = 1;
/** /**
* The project reference. * The project reference.
*/ */
private BaseProject project_; private BaseProject project;
/** /**
* Adds paths to source files, or directories containing source files to analyze. * Adds paths to source files, or directories containing source files to analyze.
@ -122,7 +122,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #inputPaths(Path...) * @see #inputPaths(Path...)
*/ */
public PmdOperation addInputPath(Path... inputPath) { public PmdOperation addInputPath(Path... inputPath) {
inputPaths_.addAll(List.of(inputPath)); inputPaths.addAll(List.of(inputPath));
return this; return this;
} }
@ -132,7 +132,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addRelativizeRoot(Path...) * @see #addRelativizeRoot(Path...)
*/ */
public PmdOperation addRelativizeRoot(Path... relativeRoot) { public PmdOperation addRelativizeRoot(Path... relativeRoot) {
relativizeRoots_.addAll(List.of(relativeRoot)); this.relativizeRoots.addAll(List.of(relativeRoot));
return this; return this;
} }
@ -155,7 +155,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #ruleSets(String...) * @see #ruleSets(String...)
*/ */
public PmdOperation addRuleSet(String... ruleSet) { public PmdOperation addRuleSet(String... ruleSet) {
ruleSets_.addAll(List.of(ruleSet)); ruleSets.addAll(List.of(ruleSet));
return this; return this;
} }
@ -177,8 +177,8 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* *
* @see #ruleSets(Collection) * @see #ruleSets(Collection)
*/ */
public PmdOperation addRuleSet(Collection<String> ruleSets) { public PmdOperation addRuleSet(Collection<String> ruleSet) {
ruleSets_.addAll(ruleSets); ruleSets.addAll(ruleSet);
return this; return this;
} }
@ -186,7 +186,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets the location of the cache file for incremental analysis. * Sets the location of the cache file for incremental analysis.
*/ */
public PmdOperation cache(Path cache) { public PmdOperation cache(Path cache) {
cache_ = cache; this.cache = cache;
return this; return this;
} }
@ -194,7 +194,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets the default language to be used for all input files. * Sets the default language to be used for all input files.
*/ */
public PmdOperation defaultLanguage(LanguageVersion... languageVersion) { public PmdOperation defaultLanguage(LanguageVersion... languageVersion) {
languageVersions_.addAll(List.of(languageVersion)); this.languageVersions.addAll(List.of(languageVersion));
return this; return this;
} }
@ -204,7 +204,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* <p>The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}.</p> * <p>The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}.</p>
*/ */
public PmdOperation encoding(String encoding) { public PmdOperation encoding(String encoding) {
encoding = encoding; this.encoding = encoding;
return this; return this;
} }
@ -213,11 +213,11 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*/ */
@Override @Override
public void execute() { public void execute() {
if (project_ == null) { if (project == null) {
throw new IllegalArgumentException("ERROR: project required."); throw new IllegalArgumentException("ERROR: project required.");
} }
var commandName = project_.getCurrentCommandName(); var commandName = project.getCurrentCommandName();
performPmdAnalysis(commandName, initConfiguration(commandName)); performPmdAnalysis(commandName, initConfiguration(commandName));
} }
@ -225,7 +225,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets whether the build will continue on warnings. * Sets whether the build will continue on warnings.
*/ */
public PmdOperation failOnViolation(boolean failOnViolation) { public PmdOperation failOnViolation(boolean failOnViolation) {
failOnViolation_ = failOnViolation; this.failOnViolation = failOnViolation;
return this; return this;
} }
@ -233,7 +233,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Forces a language to be used for all input files, irrespective of file names. * Forces a language to be used for all input files, irrespective of file names.
*/ */
public PmdOperation forceVersion(LanguageVersion languageVersion) { public PmdOperation forceVersion(LanguageVersion languageVersion) {
forcedLanguageVersion_ = languageVersion; this.forcedLanguageVersion = languageVersion;
return this; return this;
} }
@ -255,11 +255,11 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* </ul> * </ul>
*/ */
public PmdOperation fromProject(BaseProject project) { public PmdOperation fromProject(BaseProject project) {
project_ = project; this.project = project;
inputPaths_.add(project.srcMainDirectory().toPath()); inputPaths.add(project.srcMainDirectory().toPath());
inputPaths_.add(project.srcTestDirectory().toPath()); inputPaths.add(project.srcTestDirectory().toPath());
ruleSets_.add(RULE_SET_DEFAULT); ruleSets.add(RULE_SET_DEFAULT);
return this; return this;
} }
@ -267,7 +267,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets the path to the file containing a list of files to ignore, one path per line. * Sets the path to the file containing a list of files to ignore, one path per line.
*/ */
public PmdOperation ignoreFile(Path ignoreFile) { public PmdOperation ignoreFile(Path ignoreFile) {
ignoreFile_ = ignoreFile; this.ignoreFile = ignoreFile;
return this; return this;
} }
@ -275,7 +275,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Enables or disables incremental analysis. * Enables or disables incremental analysis.
*/ */
public PmdOperation incrementalAnalysis(boolean incrementalAnalysis) { public PmdOperation incrementalAnalysis(boolean incrementalAnalysis) {
incrementalAnalysis_ = incrementalAnalysis; this.incrementalAnalysis = incrementalAnalysis;
return this; return this;
} }
@ -285,55 +285,55 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
public PMDConfiguration initConfiguration(String commandName) { public PMDConfiguration initConfiguration(String commandName) {
PMDConfiguration config = new PMDConfiguration(); PMDConfiguration config = new PMDConfiguration();
if (cache_ == null && project_ != null && incrementalAnalysis_) { if (cache == null && project != null && incrementalAnalysis) {
config.setAnalysisCacheLocation( config.setAnalysisCacheLocation(
Paths.get(project_.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-cache").toFile().getAbsolutePath()); Paths.get(project.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-cache").toFile().getAbsolutePath());
} else if (cache_ != null) { } else if (cache != null) {
config.setAnalysisCacheLocation(cache_.toFile().getAbsolutePath()); config.setAnalysisCacheLocation(cache.toFile().getAbsolutePath());
} }
config.setFailOnViolation(failOnViolation_); config.setFailOnViolation(failOnViolation);
if (languageVersions_ != null) { if (languageVersions != null) {
config.setDefaultLanguageVersions(languageVersions_); config.setDefaultLanguageVersions(languageVersions);
} }
if (forcedLanguageVersion_ != null) { if (forcedLanguageVersion != null) {
config.setForceLanguageVersion(forcedLanguageVersion_); config.setForceLanguageVersion(forcedLanguageVersion);
} }
if (ignoreFile_ != null) { if (ignoreFile != null) {
config.setIgnoreFilePath(ignoreFile_); config.setIgnoreFilePath(ignoreFile);
} }
config.setIgnoreIncrementalAnalysis(!incrementalAnalysis_); config.setIgnoreIncrementalAnalysis(!incrementalAnalysis);
if (inputPaths_.isEmpty()) { if (inputPaths.isEmpty()) {
throw new IllegalArgumentException(commandName + ": InputPaths required."); throw new IllegalArgumentException(commandName + ": InputPaths required.");
} else { } else {
config.setInputPathList(inputPaths_); config.setInputPathList(inputPaths);
} }
if (inputUri_ != null) { if (inputUri != null) {
config.setInputUri(inputUri_); config.setInputUri(inputUri);
} }
config.setMinimumPriority(rulePriority_); config.setMinimumPriority(rulePriority);
if (project_ != null) { if (project != null) {
config.setReportFile(Objects.requireNonNullElseGet(reportFile_, config.setReportFile(Objects.requireNonNullElseGet(reportFile,
() -> Paths.get(project_.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-report." + reportFormat_))); () -> Paths.get(project.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-report." + reportFormat)));
} else { } else {
config.setReportFile(reportFile_); config.setReportFile(reportFile);
} }
config.addRelativizeRoots(relativizeRoots_); config.addRelativizeRoots(relativizeRoots);
config.setReportFormat(reportFormat_); config.setReportFormat(reportFormat);
config.setRuleSets(ruleSets_); config.setRuleSets(ruleSets);
config.setShowSuppressedViolations(showSuppressed_); config.setShowSuppressedViolations(showSuppressed);
config.setSourceEncoding(encoding); config.setSourceEncoding(encoding);
config.setSuppressMarker(suppressedMarker_); config.setSuppressMarker(suppressedMarker);
config.setThreads(threads_); config.setThreads(threads);
return config; return config;
} }
@ -345,8 +345,8 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addInputPath(Path...) * @see #addInputPath(Path...)
*/ */
public PmdOperation inputPaths(Path... inputPath) { public PmdOperation inputPaths(Path... inputPath) {
inputPaths_.clear(); inputPaths.clear();
inputPaths_.addAll(List.of(inputPath)); inputPaths.addAll(List.of(inputPath));
return this; return this;
} }
@ -357,8 +357,8 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
var pmd = PmdAnalysis.create(config); var pmd = PmdAnalysis.create(config);
var report = pmd.performAnalysisAndCollectReport(); var report = pmd.performAnalysisAndCollectReport();
if (LOGGER.isLoggable(Level.INFO)) { if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "[{0}] inputPaths{1}", new Object[]{commandName, inputPaths_}); LOGGER.log(Level.INFO, "[{0}] inputPaths{1}", new Object[]{commandName, inputPaths});
LOGGER.log(Level.INFO, "[{0}] ruleSets{1}", new Object[]{commandName, ruleSets_}); LOGGER.log(Level.INFO, "[{0}] ruleSets{1}", new Object[]{commandName, ruleSets});
} }
var numErrors = report.getViolations().size(); var numErrors = report.getViolations().size();
if (numErrors > 0) { if (numErrors > 0) {
@ -368,9 +368,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
for (var v : report.getViolations()) { for (var v : report.getViolations()) {
if (LOGGER.isLoggable(Level.WARNING)) { if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, "[{0}] {1}:{2}:\n\t{3} ({4})\n\t\t--> {5}", LOGGER.log(Level.WARNING, "[{0}] {1}:{2}:\n\t{3} ({4})\n\t\t--> {5}",
new Object[]{commandName, new Object[]{commandName, Paths.get(v.getFileId().getFileName()).toUri(), v.getBeginLine(),
v.getFileId().getAbsolutePath(),
v.getBeginLine(),
v.getRule().getName(), v.getRule().getName(),
v.getRule().getExternalInfoUrl() //TODO bug in PMD? v.getRule().getExternalInfoUrl() //TODO bug in PMD?
.replace("${pmd.website.baseurl}", .replace("${pmd.website.baseurl}",
@ -406,8 +404,8 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addRelativizeRoot(Path...) * @see #addRelativizeRoot(Path...)
*/ */
public PmdOperation relativizeRoots(Path... relativeRoot) { public PmdOperation relativizeRoots(Path... relativeRoot) {
relativizeRoots_.clear(); this.relativizeRoots.clear();
relativizeRoots_.addAll(List.of(relativeRoot)); this.relativizeRoots.addAll(List.of(relativeRoot));
return this; return this;
} }
@ -415,7 +413,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets the output format of the analysis report. The default is {@code text}. * Sets the output format of the analysis report. The default is {@code text}.
*/ */
public PmdOperation reportFormat(String reportFormat) { public PmdOperation reportFormat(String reportFormat) {
reportFormat_ = reportFormat; this.reportFormat = reportFormat;
return this; return this;
} }
@ -438,8 +436,8 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addRuleSet(String...) * @see #addRuleSet(String...)
*/ */
public PmdOperation ruleSets(String... ruleSet) { public PmdOperation ruleSets(String... ruleSet) {
ruleSets_.clear(); ruleSets.clear();
ruleSets_.addAll(Arrays.asList(ruleSet)); ruleSets.addAll(Arrays.asList(ruleSet));
return this; return this;
} }
@ -461,9 +459,9 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* *
* @see #addRuleSet(Collection) * @see #addRuleSet(Collection)
*/ */
public PmdOperation ruleSets(Collection<String> ruleSets) { public PmdOperation ruleSets(Collection<String> ruleSet) {
ruleSets_.clear(); ruleSets.clear();
ruleSets_.addAll(ruleSets); ruleSets.addAll(ruleSet);
return this; return this;
} }
@ -471,7 +469,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Enables or disables adding the suppressed rule violations to the report. * Enables or disables adding the suppressed rule violations to the report.
*/ */
public PmdOperation showSuppressed(boolean showSuppressed) { public PmdOperation showSuppressed(boolean showSuppressed) {
showSuppressed_ = showSuppressed; this.showSuppressed = showSuppressed;
return this; return this;
} }
@ -479,7 +477,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Specifies the comment token that marks lines which should be ignored. The default is {@code NOPMD}. * Specifies the comment token that marks lines which should be ignored. The default is {@code NOPMD}.
*/ */
public PmdOperation suppressedMarker(String suppressedMarker) { public PmdOperation suppressedMarker(String suppressedMarker) {
suppressedMarker_ = suppressedMarker; this.suppressedMarker = suppressedMarker;
return this; return this;
} }
@ -487,7 +485,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets the number of threads to be used. The default is {code 1}. * Sets the number of threads to be used. The default is {code 1}.
*/ */
public PmdOperation threads(int threads) { public PmdOperation threads(int threads) {
threads_ = threads; this.threads = threads;
return this; return this;
} }
@ -495,7 +493,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* Sets the input URI to process for source code objects. * Sets the input URI to process for source code objects.
*/ */
public PmdOperation uri(URI inputUri) { public PmdOperation uri(URI inputUri) {
inputUri_ = inputUri; this.inputUri = inputUri;
return this; return this;
} }
} }

View file

@ -49,9 +49,9 @@ class PmdOperationTest {
PmdOperation newPmdOperation() { PmdOperation newPmdOperation() {
final PmdOperation pmdOperation = new PmdOperation(); final PmdOperation pmdOperation = new PmdOperation();
pmdOperation.inputPaths(Path.of("src/main"), Path.of("src/test")); pmdOperation.inputPaths(Path.of("src/main"), Path.of("src/test"));
pmdOperation.reportFile_ = Paths.get("build", COMMAND_NAME, "pmd-test-report.txt"); pmdOperation.reportFile = Paths.get("build", COMMAND_NAME, "pmd-test-report.txt");
pmdOperation.cache_ = Paths.get("build", COMMAND_NAME, "pmd-cache"); pmdOperation.cache = Paths.get("build", COMMAND_NAME, "pmd-cache");
pmdOperation.failOnViolation_ = false; pmdOperation.failOnViolation = false;
return pmdOperation; return pmdOperation;
} }
@ -194,7 +194,7 @@ class PmdOperationTest {
void testReportFormat() throws IOException { void testReportFormat() throws IOException {
var pmd = newPmdOperation().ruleSets(ERROR_PRONE).reportFormat("xml").inputPaths(ERROR_PRONE_SAMPLE); var pmd = newPmdOperation().ruleSets(ERROR_PRONE).reportFormat("xml").inputPaths(ERROR_PRONE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isEqualTo(ERROR_PRONE_ERRORS); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isEqualTo(ERROR_PRONE_ERRORS);
try (var br = Files.newBufferedReader(pmd.reportFile_)) { try (var br = Files.newBufferedReader(pmd.reportFile)) {
assertThat(br.readLine()).as("xml report").startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); assertThat(br.readLine()).as("xml report").startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
} }
} }