diff --git a/.gitignore b/.gitignore index 6449503..a63b224 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,4 @@ atlassian-ide-plugin.xml .idea/sonarlint/ # Editor-based Rest Client -.idea/httpRequests - -local.properties \ No newline at end of file +.idea/httpRequests \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index ce88027..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,284 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml index 79ee123..a55e7a1 100644 --- a/.idea/codeStyles/codeStyleConfig.xml +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -1,5 +1,5 @@ - \ No newline at end of file diff --git a/README.md b/README.md index f99eda9..9e985d2 100755 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ [![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) -[![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) To check all source code using the [java quickstart rule](https://pmd.github.io/pmd/pmd_rules_java.html). diff --git a/src/bld/java/rife/bld/extension/PmdOperationBuild.java b/src/bld/java/rife/bld/extension/PmdOperationBuild.java index f21951d..546555f 100644 --- a/src/bld/java/rife/bld/extension/PmdOperationBuild.java +++ b/src/bld/java/rife/bld/extension/PmdOperationBuild.java @@ -1,28 +1,22 @@ package rife.bld.extension; import rife.bld.Project; -import rife.bld.publish.PublishDeveloper; -import rife.bld.publish.PublishLicense; -import rife.bld.publish.PublishScm; import java.util.List; -import static rife.bld.dependencies.Repository.*; +import static rife.bld.dependencies.Repository.MAVEN_CENTRAL; +import static rife.bld.dependencies.Repository.RIFE2_RELEASES; import static rife.bld.dependencies.Scope.*; -import static rife.bld.operations.JavadocOptions.DocLinkOption.NO_MISSING; - public class PmdOperationBuild extends Project { public PmdOperationBuild() { pkg = "rife.bld.extension"; - name = "bld-pmd"; + name = "PmdOperation"; + mainClass = "rife.bld.extension.PmdOperationMain"; version = version(0, 9, 0, "SNAPSHOT"); - javaRelease = 17; downloadSources = true; - autoDownloadPurge = true; repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES); - scope(compile) .include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 20))) .include(dependency("net.sourceforge.pmd:pmd-java:6.55.0")); @@ -32,30 +26,6 @@ public class PmdOperationBuild extends Project { .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2))) .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 9, 2))) .include(dependency("org.assertj:assertj-joda-time:2.2.0")); - - javadocOperation() - .javadocOptions() - .docLint(NO_MISSING) - .link("https://rife2.github.io/rife2/") - .link("https://javadoc.io/doc/net.sourceforge.pmd/pmd-core/latest/"); - - publishOperation() -// .repository(MAVEN_LOCAL) - .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2")) - .info() - .groupId("com.uwyn.rife2") - .artifactId("bld-pmd") - .description("bld Extension to Perform Static Code Analysis with PMD") - .url("https://github.com/rife2/bld-pmd") - .developer(new PublishDeveloper().id("ethauvin").name("Erik C. Thauvin").email("erik@thauvin.net") - .url("https://erik.thauvin.net/")) - .license(new PublishLicense().name("The Apache License, Version 2.0") - .url("http://www.apache.org/licenses/LICENSE-2.0.txt")) - .scm(new PublishScm().connection("scm:git:https://github.com/rife2/bld-pmd.git") - .developerConnection("scm:git:git@github.com:rife2/bld-pmd.git") - .url("https://github.com/rife2/bld-pmd")) - .signKey(property("sign.key")) - .signPassphrase(property("sign.passphrase")); } public static void main(String[] args) { diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index 5e5faf8..1ef78b2 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -124,6 +124,32 @@ public class PmdOperation extends AbstractOperation { */ private BaseProject project; + /** + * Configures a PMD operation from a {@link BaseProject}. + * + *

+ * The defaults are: + *

+ */ + 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. * @@ -135,7 +161,7 @@ public class PmdOperation extends AbstractOperation { } /** - * Adds several paths to shorten paths that are output in the report. + * Add several paths to shorten paths that are output in the report. * * @see #addRelativizeRoot(Path...) */ @@ -232,32 +258,6 @@ public class PmdOperation extends AbstractOperation { return this; } - /** - * Configures a PMD operation from a {@link BaseProject}. - * - *

- * The defaults are: - *

    - *
  • cache={@code build/pmd/pmd-cache}
  • - *
  • encoding={@code UTF-9}
  • - *
  • incrementAnalysis={@code true}
  • - *
  • inputPaths={@code [src/main, src/test]}
  • - *
  • reportFile={@code build/pmd/pmd-report-txt}
  • - *
  • reportFormat={@code text}
  • - *
  • rulePriority={@code LOW}
  • - *
  • ruleSets={@code [rulesets/java/quickstart.xml]}
  • - *
  • suppressedMarker={@code NOPMD}
  • - *
- */ - 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. */ @@ -363,8 +363,7 @@ public class PmdOperation extends AbstractOperation { for (var v : report.getViolations()) { if (LOGGER.isLoggable(Level.WARNING)) { 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()) { diff --git a/src/test/java/rife/bld/extension/PmdOperationTest.java b/src/test/java/rife/bld/extension/PmdOperationTest.java index af81331..50d7ad0 100644 --- a/src/test/java/rife/bld/extension/PmdOperationTest.java +++ b/src/test/java/rife/bld/extension/PmdOperationTest.java @@ -36,7 +36,6 @@ import static org.assertj.core.api.Assertions.assertThatCode; * @since 1.0 */ class PmdOperationTest { - static final String CATEGORY_FOO = "category/foo.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 int CODING_STYLE_ERRORS = 16; @@ -45,6 +44,7 @@ class PmdOperationTest { static final int ERROR_PRONE_ERRORS = 8; static final Path ERROR_PRONE_SAMPLE = Path.of("src/test/resources/java/ErrorProne.java"); static final String TEST = "test"; + static final String CATEGORY_FOO = "category/foo.xml"; PmdOperation newPmdOperation() { final PmdOperation pmdOperation = new PmdOperation(); diff --git a/src/test/resources/java/BestPractices.java b/src/test/resources/java/BestPractices.java index 904b4e0..36cbb4a 100644 --- a/src/test/resources/java/BestPractices.java +++ b/src/test/resources/java/BestPractices.java @@ -23,15 +23,15 @@ package java; * @since 1.0 */ public class BestPractices { - private final String ip = "127.0.0.1"; // not recommended - private StringBuffer buffer; // potential memory leak as an instance variable; + private final String ip = "127.0.0.1"; // not recommended + private StringBuffer buffer; // potential memory leak as an instance variable; private String[] x; void bar(int a) { switch (a) { - case 1: // do something + case 1: // do something break; - default: // the default case should be last, by convention + default: // the default case should be last, by convention break; case 2: break; @@ -47,4 +47,6 @@ public class BestPractices { name = name.trim(); System.out.println("Hello " + name); } + + } diff --git a/src/test/resources/java/CodeStyle.java b/src/test/resources/java/CodeStyle.java index 740193a..98d1525 100644 --- a/src/test/resources/java/CodeStyle.java +++ b/src/test/resources/java/CodeStyle.java @@ -25,7 +25,7 @@ package java; public final class CodeStyle { final int FinalField = 1; 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 diff --git a/src/test/resources/java/Design.java b/src/test/resources/java/Design.java index c177aa0..e430e9d 100644 --- a/src/test/resources/java/Design.java +++ b/src/test/resources/java/Design.java @@ -26,6 +26,10 @@ public class Design { String field; int otherField; + void foo() { + throw new NullPointerException(); + } + public void bar(int x, int y, int z) { if (x > y) { if (y > z) { @@ -35,8 +39,4 @@ public class Design { } } } - - void foo() { - throw new NullPointerException(); - } } diff --git a/src/test/resources/java/ErrorProne.java b/src/test/resources/java/ErrorProne.java index bdb8494..b787d97 100644 --- a/src/test/resources/java/ErrorProne.java +++ b/src/test/resources/java/ErrorProne.java @@ -37,18 +37,18 @@ public class ErrorProne { } } + void foo() { + try { + // do something + } catch (Throwable th) { // should not catch Throwable + th.printStackTrace(); + } + } + void bar() { try { // do something } catch (NullPointerException npe) { } } - - void foo() { - try { - // do something - } catch (Throwable th) { // should not catch Throwable - th.printStackTrace(); - } - } } diff --git a/src/test/resources/java/MultiThreading.java b/src/test/resources/java/MultiThreading.java index 577a4aa..f2c8161 100644 --- a/src/test/resources/java/MultiThreading.java +++ b/src/test/resources/java/MultiThreading.java @@ -33,6 +33,12 @@ public class MultiThreading { sdf.format("bar"); // poor, no thread-safety } + void foo() { + synchronized (sdf) { // preferred + sdf.format("foo"); + } + } + Object obj() { if (baz == null) { // baz may be non-null yet not fully created synchronized (this) { diff --git a/src/test/resources/java/Performance.java b/src/test/resources/java/Performance.java index 7882194..e00f30f 100644 --- a/src/test/resources/java/Performance.java +++ b/src/test/resources/java/Performance.java @@ -29,6 +29,7 @@ public class Performance { } } + private boolean checkTrimEmpty(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isWhitespace(str.charAt(i))) { @@ -40,7 +41,7 @@ public class Performance { void foo() { StringBuffer sb = new StringBuffer(); - sb.append("a"); // avoid this + sb.append("a"); // avoid this String foo = " "; StringBuffer buf = new StringBuffer(); @@ -50,6 +51,7 @@ public class Performance { buf.append("Hello").append(" ").append("World"); - StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir")); // poor + StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir")); +// poor } }