Minor cleanup

This commit is contained in:
Erik C. Thauvin 2023-04-15 09:27:30 -07:00
parent e2126624f4
commit 73eb6333d2
12 changed files with 338 additions and 61 deletions

284
.idea/codeStyles/Project.xml generated Normal file
View file

@ -0,0 +1,284 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<codeStyleSettings language="JAVA">
<option name="IF_BRACE_FORCE" value="1" />
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PUBLIC>true</PUBLIC>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PROTECTED>true</PROTECTED>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PRIVATE>true</PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PUBLIC>true</PUBLIC>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PROTECTED>true</PROTECTED>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PRIVATE>true</PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<INITIALIZER_BLOCK>true</INITIALIZER_BLOCK>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PUBLIC>true</PUBLIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PROTECTED>true</PROTECTED>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PRIVATE>true</PRIVATE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PUBLIC>true</PUBLIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PROTECTED>true</PROTECTED>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PRIVATE>true</PRIVATE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<FIELD>true</FIELD>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<INITIALIZER_BLOCK>true</INITIALIZER_BLOCK>
</match>
</rule>
</section>
<section>
<rule>
<match>
<CONSTRUCTOR>true</CONSTRUCTOR>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<METHOD>true</METHOD>
<STATIC>true</STATIC>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<METHOD>true</METHOD>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<ENUM>true</ENUM>
</match>
</rule>
</section>
<section>
<rule>
<match>
<INTERFACE>true</INTERFACE>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<CLASS>true</CLASS>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<CLASS>true</CLASS>
</match>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</code_scheme>
</component>

View file

@ -1,5 +1,5 @@
<component name="ProjectCodeStyleConfiguration"> <component name="ProjectCodeStyleConfiguration">
<state> <state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" /> <option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state> </state>
</component> </component>

View file

@ -3,6 +3,8 @@
[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](http://opensource.org/licenses/BSD-3-Clause) [![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](http://opensource.org/licenses/BSD-3-Clause)
[![Java](https://img.shields.io/badge/java-17%2B-blue)](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) [![Java](https://img.shields.io/badge/java-17%2B-blue)](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
[![Release](https://flat.badgen.net/maven/v/metadata-url/repo.rife2.com/releases/com/uwyn/rife2/bld-pmd/maven-metadata.xml?color=blue)](https://repo.rife2.com/#/releases/com/uwyn/rife2/bld-pmd)
[![Snapshot](https://flat.badgen.net/maven/v/metadata-url/repo.rife2.com/snapshots/com/uwyn/rife2/bld-pmd/maven-metadata.xml?label=snapshot)](https://repo.rife2.com/#/snapshots/com/uwyn/rife2/bld-pmd)
[![GitHub CI](https://github.com/rife2/bld-pmd/actions/workflows/bld.yml/badge.svg)](https://github.com/rife2/bld-pmd/actions/workflows/bld.yml) [![GitHub CI](https://github.com/rife2/bld-pmd/actions/workflows/bld.yml/badge.svg)](https://github.com/rife2/bld-pmd/actions/workflows/bld.yml)
To check all source code using the [java quickstart rule](https://pmd.github.io/pmd/pmd_rules_java.html). To check all source code using the [java quickstart rule](https://pmd.github.io/pmd/pmd_rules_java.html).

View file

@ -40,8 +40,8 @@ public class PmdOperationBuild extends Project {
.link("https://javadoc.io/doc/net.sourceforge.pmd/pmd-core/latest/"); .link("https://javadoc.io/doc/net.sourceforge.pmd/pmd-core/latest/");
publishOperation() publishOperation()
.repository(MAVEN_LOCAL) // .repository(MAVEN_LOCAL)
// .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2")) .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2"))
.info() .info()
.groupId("com.uwyn.rife2") .groupId("com.uwyn.rife2")
.artifactId("bld-pmd") .artifactId("bld-pmd")

View file

@ -124,32 +124,6 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*/ */
private BaseProject project; private BaseProject project;
/**
* Configures a PMD operation from a {@link BaseProject}.
*
* <p>
* The defaults are:
* <ul>
* <li>cache={@code build/pmd/pmd-cache}</li>
* <li>encoding={@code UTF-9}</li>
* <li>incrementAnalysis={@code true}</li>
* <li>inputPaths={@code [src/main, src/test]}</li>
* <li>reportFile={@code build/pmd/pmd-report-txt}</li>
* <li>reportFormat={@code text}</li>
* <li>rulePriority={@code LOW}</li>
* <li>ruleSets={@code [rulesets/java/quickstart.xml]}</li>
* <li>suppressedMarker={@code NOPMD}</li>
* </ul>
*/
public PmdOperation fromProject(BaseProject project) {
this.project = project;
inputPaths.add(project.srcMainDirectory().toPath());
inputPaths.add(project.srcTestDirectory().toPath());
ruleSets.add(RULE_SET_DEFAULT);
return this;
}
/** /**
* Adds paths to source files, or directories containing source files to analyze. * Adds paths to source files, or directories containing source files to analyze.
* *
@ -161,7 +135,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
} }
/** /**
* Add several paths to shorten paths that are output in the report. * Adds several paths to shorten paths that are output in the report.
* *
* @see #addRelativizeRoot(Path...) * @see #addRelativizeRoot(Path...)
*/ */
@ -258,6 +232,32 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
return this; return this;
} }
/**
* Configures a PMD operation from a {@link BaseProject}.
*
* <p>
* The defaults are:
* <ul>
* <li>cache={@code build/pmd/pmd-cache}</li>
* <li>encoding={@code UTF-9}</li>
* <li>incrementAnalysis={@code true}</li>
* <li>inputPaths={@code [src/main, src/test]}</li>
* <li>reportFile={@code build/pmd/pmd-report-txt}</li>
* <li>reportFormat={@code text}</li>
* <li>rulePriority={@code LOW}</li>
* <li>ruleSets={@code [rulesets/java/quickstart.xml]}</li>
* <li>suppressedMarker={@code NOPMD}</li>
* </ul>
*/
public PmdOperation fromProject(BaseProject project) {
this.project = project;
inputPaths.add(project.srcMainDirectory().toPath());
inputPaths.add(project.srcTestDirectory().toPath());
ruleSets.add(RULE_SET_DEFAULT);
return this;
}
/** /**
* 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.
*/ */
@ -363,7 +363,8 @@ 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.warning(String.format("[%s] %s:%d\n\t%s (%s)\n\t\t--> %s", commandName, LOGGER.warning(String.format("[%s] %s:%d\n\t%s (%s)\n\t\t--> %s", commandName,
Paths.get(v.getFilename()).toUri(), v.getBeginLine(), v.getRule().getName(), v.getRule().getExternalInfoUrl(), v.getDescription())); Paths.get(v.getFilename()).toUri(), v.getBeginLine(), v.getRule().getName(),
v.getRule().getExternalInfoUrl(), v.getDescription()));
} }
} }
if (config.isFailOnViolation()) { if (config.isFailOnViolation()) {

View file

@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThatCode;
* @since 1.0 * @since 1.0
*/ */
class PmdOperationTest { class PmdOperationTest {
static final String CATEGORY_FOO = "category/foo.xml";
static final String CODE_STYLE = "category/java/codestyle.xml"; static final String CODE_STYLE = "category/java/codestyle.xml";
static final Path CODE_STYLE_SAMPLE = Path.of("src/test/resources/java/CodeStyle.java"); static final Path CODE_STYLE_SAMPLE = Path.of("src/test/resources/java/CodeStyle.java");
static final int CODING_STYLE_ERRORS = 16; static final int CODING_STYLE_ERRORS = 16;
@ -44,7 +45,6 @@ class PmdOperationTest {
static final int ERROR_PRONE_ERRORS = 8; static final int ERROR_PRONE_ERRORS = 8;
static final Path ERROR_PRONE_SAMPLE = Path.of("src/test/resources/java/ErrorProne.java"); static final Path ERROR_PRONE_SAMPLE = Path.of("src/test/resources/java/ErrorProne.java");
static final String TEST = "test"; static final String TEST = "test";
static final String CATEGORY_FOO = "category/foo.xml";
PmdOperation newPmdOperation() { PmdOperation newPmdOperation() {
final PmdOperation pmdOperation = new PmdOperation(); final PmdOperation pmdOperation = new PmdOperation();

View file

@ -23,15 +23,15 @@ package java;
* @since 1.0 * @since 1.0
*/ */
public class BestPractices { public class BestPractices {
private final String ip = "127.0.0.1"; // not recommended private final String ip = "127.0.0.1"; // not recommended
private StringBuffer buffer; // potential memory leak as an instance variable; private StringBuffer buffer; // potential memory leak as an instance variable;
private String[] x; private String[] x;
void bar(int a) { void bar(int a) {
switch (a) { switch (a) {
case 1: // do something case 1: // do something
break; break;
default: // the default case should be last, by convention default: // the default case should be last, by convention
break; break;
case 2: case 2:
break; break;
@ -47,6 +47,4 @@ public class BestPractices {
name = name.trim(); name = name.trim();
System.out.println("Hello " + name); System.out.println("Hello " + name);
} }
} }

View file

@ -25,7 +25,7 @@ package java;
public final class CodeStyle { public final class CodeStyle {
final int FinalField = 1; final int FinalField = 1;
private int x; private int x;
private int y; // class cannot be subclassed, so is y really private or package visible? private int y; // class cannot be subclassed, so is y really private or package visible?
// missing constructor // missing constructor

View file

@ -26,10 +26,6 @@ public class Design {
String field; String field;
int otherField; int otherField;
void foo() {
throw new NullPointerException();
}
public void bar(int x, int y, int z) { public void bar(int x, int y, int z) {
if (x > y) { if (x > y) {
if (y > z) { if (y > z) {
@ -39,4 +35,8 @@ public class Design {
} }
} }
} }
void foo() {
throw new NullPointerException();
}
} }

View file

@ -37,18 +37,18 @@ public class ErrorProne {
} }
} }
void foo() {
try {
// do something
} catch (Throwable th) { // should not catch Throwable
th.printStackTrace();
}
}
void bar() { void bar() {
try { try {
// do something // do something
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
} }
} }
void foo() {
try {
// do something
} catch (Throwable th) { // should not catch Throwable
th.printStackTrace();
}
}
} }

View file

@ -33,12 +33,6 @@ public class MultiThreading {
sdf.format("bar"); // poor, no thread-safety sdf.format("bar"); // poor, no thread-safety
} }
void foo() {
synchronized (sdf) { // preferred
sdf.format("foo");
}
}
Object obj() { Object obj() {
if (baz == null) { // baz may be non-null yet not fully created if (baz == null) { // baz may be non-null yet not fully created
synchronized (this) { synchronized (this) {

View file

@ -29,7 +29,6 @@ public class Performance {
} }
} }
private boolean checkTrimEmpty(String str) { private boolean checkTrimEmpty(String str) {
for (int i = 0; i < str.length(); i++) { for (int i = 0; i < str.length(); i++) {
if (!Character.isWhitespace(str.charAt(i))) { if (!Character.isWhitespace(str.charAt(i))) {
@ -41,7 +40,7 @@ public class Performance {
void foo() { void foo() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append("a"); // avoid this sb.append("a"); // avoid this
String foo = " "; String foo = " ";
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
@ -51,7 +50,6 @@ public class Performance {
buf.append("Hello").append(" ").append("World"); buf.append("Hello").append(" ").append("World");
StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir")); StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir")); // poor
// poor
} }
} }