From cd2efd3348a20de5352590e7be9d9a4476d180ef Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 8 Mar 2023 21:36:30 +0400 Subject: [PATCH 1/2] Improve LicenseHeader default regex for Java --- .../spotless/generic/LicenseHeaderStep.java | 1 + .../gradle/spotless/JavaExtension.java | 4 +--- .../spotless/maven/groovy/Groovy.java | 3 ++- .../diffplug/spotless/maven/java/Java.java | 3 ++- .../diffplug/spotless/maven/scala/Scala.java | 3 ++- .../resources/license/HelloWorld_java.test | 5 +++++ .../license/HelloWorld_withImport_java.test | 7 +++++++ .../main/resources/license/module-info.test | 5 +++++ .../generic/LicenseHeaderStepTest.java | 20 ++++++++++++++++++- 9 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 testlib/src/main/resources/license/HelloWorld_java.test create mode 100644 testlib/src/main/resources/license/HelloWorld_withImport_java.test create mode 100644 testlib/src/main/resources/license/module-info.test diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index b4c0cf2a6f..3b0bf829f8 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -45,6 +45,7 @@ /** Prefixes a license header before the package statement. */ public final class LicenseHeaderStep { + public static final String DEFAULT_JAVA_HEADER_DELIMITER = "(package|import|public|class|module) "; private static final Logger LOGGER = LoggerFactory.getLogger(LicenseHeaderStep.class); public enum YearMode { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index f351dd6188..bfe1a96955 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -50,9 +50,7 @@ public JavaExtension(SpotlessExtension spotless) { super(spotless); } - // If this constant changes, don't forget to change the similarly-named one in - // testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java as well - static final String LICENSE_HEADER_DELIMITER = "package "; + static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public LicenseHeaderConfig licenseHeader(String licenseHeader) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java index 8041b812f9..35b91f3677 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java @@ -20,6 +20,7 @@ import org.apache.maven.project.MavenProject; import com.diffplug.common.collect.ImmutableSet; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterFactory; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -32,7 +33,7 @@ public class Groovy extends FormatterFactory { private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/groovy/**/*.groovy", "src/test/groovy/**/*.groovy"); - private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public Set defaultIncludes(MavenProject project) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index efdf0827db..b0692446b7 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -26,6 +26,7 @@ import org.apache.maven.model.Build; import org.apache.maven.project.MavenProject; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterFactory; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -37,7 +38,7 @@ */ public class Java extends FormatterFactory { - private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public Set defaultIncludes(MavenProject project) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java index 7a9e455f5f..116d89263c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java @@ -20,6 +20,7 @@ import org.apache.maven.project.MavenProject; import com.diffplug.common.collect.ImmutableSet; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterFactory; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -33,7 +34,7 @@ public class Scala extends FormatterFactory { private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/scala/**/*.scala", "src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc"); - private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public Set defaultIncludes(MavenProject project) { diff --git a/testlib/src/main/resources/license/HelloWorld_java.test b/testlib/src/main/resources/license/HelloWorld_java.test new file mode 100644 index 0000000000..d9be2ea939 --- /dev/null +++ b/testlib/src/main/resources/license/HelloWorld_java.test @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.print("Hello World"); + } +} diff --git a/testlib/src/main/resources/license/HelloWorld_withImport_java.test b/testlib/src/main/resources/license/HelloWorld_withImport_java.test new file mode 100644 index 0000000000..fcfb64117e --- /dev/null +++ b/testlib/src/main/resources/license/HelloWorld_withImport_java.test @@ -0,0 +1,7 @@ +import java.time.LocalDate; + +public class HelloWorld { + public static void main(String[] args) { + System.out.print("Hello World. Date: " + LocalDate.now()); + } +} diff --git a/testlib/src/main/resources/license/module-info.test b/testlib/src/main/resources/license/module-info.test new file mode 100644 index 0000000000..6678147b68 --- /dev/null +++ b/testlib/src/main/resources/license/module-info.test @@ -0,0 +1,5 @@ +module java.sql { + exports java.sql; + exports javax.sql; + exports javax.transaction.xa; +} diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 2b1d0aff07..912975dda5 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -32,7 +32,7 @@ class LicenseHeaderStepTest extends ResourceHarness { private static final String FILE_NO_LICENSE = "license/FileWithoutLicenseHeader.test"; - private static final String package_ = "package "; + private static final String package_ = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp."; private static final String HEADER_WITH_RANGE_TO_$YEAR = "This is a fake license with range, 2009-$YEAR. ACME corp."; @@ -250,4 +250,22 @@ void should_preserve_year_for_license_with_address() throws Throwable { hasHeader(licenceWithAddress().replace("$YEAR", "2015").replace("FooBar Inc. All", "FooBar Inc. All")), hasHeader(licenceWithAddress().replace("$YEAR", "2015"))); } + + @Test + void noPackage() throws Throwable { + String header = header(getTestResource("license/TestLicense")); + FormatterStep step = LicenseHeaderStep.headerDelimiter(header, package_).build(); + StepHarness.forStep(step) + .test(ResourceHarness.getTestResource("license/HelloWorld_java.test"), header + ResourceHarness.getTestResource("license/HelloWorld_java.test")) + .test(ResourceHarness.getTestResource("license/HelloWorld_withImport_java.test"), header + ResourceHarness.getTestResource("license/HelloWorld_withImport_java.test")); + } + + // The following demonstrate the use of 'module' keyword + @Test + void moduleInfo() throws Throwable { + String header = header(getTestResource("license/TestLicense")); + FormatterStep step = LicenseHeaderStep.headerDelimiter(header, package_).build(); + StepHarness.forStep(step) + .test(ResourceHarness.getTestResource("license/module-info.test"), header + ResourceHarness.getTestResource("license/module-info.test")); + } } From b6cad0e4af5e5ec8b6b9a00d074447c2fa789fe0 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 10 Mar 2023 09:05:53 +0400 Subject: [PATCH 2/2] Add changelog entries --- plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 2 ++ 2 files changed, 3 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index c19a7de733..c39aff545d 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +* `licenseHeader` default pattern for Java files is updated to `(package|import|public|class|module) `. ([#1614](https://github.com/diffplug/spotless/pull/1614)) ## [6.16.0] - 2023-02-27 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 36a51d6ef2..635459368a 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* `licenseHeader` default pattern for Java files is updated to `(package|import|public|class|module) `. ([#1614](https://github.com/diffplug/spotless/pull/1614)) ## [2.34.0] - 2023-02-27 ### Added