Minor cleanup

This commit is contained in:
Erik C. Thauvin 2023-08-28 18:47:04 -07:00
parent 0397effbf9
commit 69e2b8a62e
3 changed files with 83 additions and 84 deletions

3
.idea/misc.xml generated
View file

@ -1,4 +1,3 @@
<?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">
@ -33,7 +32,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$/../bld-exec/config/pmd.xml" /> <option value="$PROJECT_DIR$/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) {
this.relativizeRoots.addAll(List.of(relativeRoot)); 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> ruleSet) { public PmdOperation addRuleSet(Collection<String> ruleSets) {
ruleSets.addAll(ruleSet); ruleSets_.addAll(ruleSets);
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) {
this.cache = cache; 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) {
this.languageVersions.addAll(List.of(languageVersion)); 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) {
this.encoding = encoding; 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) {
this.failOnViolation = failOnViolation; 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) {
this.forcedLanguageVersion = languageVersion; 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) {
this.project = project; 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) {
this.ignoreFile = ignoreFile; 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) {
this.incrementalAnalysis = incrementalAnalysis; 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) {
@ -406,8 +406,8 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addRelativizeRoot(Path...) * @see #addRelativizeRoot(Path...)
*/ */
public PmdOperation relativizeRoots(Path... relativeRoot) { public PmdOperation relativizeRoots(Path... relativeRoot) {
this.relativizeRoots.clear(); relativizeRoots_.clear();
this.relativizeRoots.addAll(List.of(relativeRoot)); relativizeRoots_.addAll(List.of(relativeRoot));
return this; return this;
} }
@ -415,7 +415,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) {
this.reportFormat = reportFormat; reportFormat_ = reportFormat;
return this; return this;
} }
@ -438,8 +438,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 +461,9 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* *
* @see #addRuleSet(Collection) * @see #addRuleSet(Collection)
*/ */
public PmdOperation ruleSets(Collection<String> ruleSet) { public PmdOperation ruleSets(Collection<String> ruleSets) {
ruleSets.clear(); ruleSets_.clear();
ruleSets.addAll(ruleSet); ruleSets_.addAll(ruleSets);
return this; return this;
} }
@ -471,7 +471,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) {
this.showSuppressed = showSuppressed; showSuppressed_ = showSuppressed;
return this; return this;
} }
@ -479,7 +479,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) {
this.suppressedMarker = suppressedMarker; suppressedMarker_ = suppressedMarker;
return this; return this;
} }
@ -487,7 +487,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) {
this.threads = threads; threads_ = threads;
return this; return this;
} }
@ -495,7 +495,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) {
this.inputUri = inputUri; 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\"?>");
} }
} }