Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LicenseHeader default regex for Java #1614

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,7 +33,7 @@
public class Groovy extends FormatterFactory {

private static final Set<String> 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<String> defaultIncludes(MavenProject project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> defaultIncludes(MavenProject project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +34,7 @@ public class Scala extends FormatterFactory {

private static final Set<String> 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<String> defaultIncludes(MavenProject project) {
Expand Down
5 changes: 5 additions & 0 deletions testlib/src/main/resources/license/HelloWorld_java.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
5 changes: 5 additions & 0 deletions testlib/src/main/resources/license/module-info.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module java.sql {
exports java.sql;
exports javax.sql;
exports javax.transaction.xa;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

Expand Down Expand Up @@ -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"));
}
}