Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Fixed
- palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685))
### Added
- Add a `forbidModuleImports` API for java ([#2679](https://github.com/diffplug/spotless/issues/2679))

## [4.0.0] - 2025-09-24
### Changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.java;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.generic.ReplaceRegexStep;

/** Forbids any module import statements. */
public final class ForbidModuleImportsStep {

/**
* Matches lines like 'import module java.base;' or 'import module java.sql;'.
*/
private static final String REGEX = "(?m)^import module[^;\\n]*;\\R?";
private static final String NAME = "forbidModuleImports";
private static final String ERROR = "Do not use module imports - replace with specific class imports as 'spotlessApply' cannot auto-fix this";

private ForbidModuleImportsStep() {}

public static FormatterStep create() {
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Fixed
- palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685))
### Added
- `forbidModuleImports()` API for java ([#2679](https://github.com/diffplug/spotless/issues/2679))

## [8.0.0] - 2025-09-24
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.diffplug.spotless.extra.java.EclipseJdtFormatterStep;
import com.diffplug.spotless.generic.LicenseHeaderStep;
import com.diffplug.spotless.java.CleanthatJavaStep;
import com.diffplug.spotless.java.ForbidModuleImportsStep;
import com.diffplug.spotless.java.ForbidWildcardImportsStep;
import com.diffplug.spotless.java.FormatAnnotationsStep;
import com.diffplug.spotless.java.GoogleJavaFormatStep;
Expand Down Expand Up @@ -162,6 +163,10 @@ public void forbidWildcardImports() {
addStep(ForbidWildcardImportsStep.create());
}

public void forbidModuleImports() {
addStep(ForbidModuleImportsStep.create());
}

/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
public GoogleJavaFormatConfig googleJavaFormat() {
return googleJavaFormat(GoogleJavaFormatStep.defaultVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ void removeWildCardImports() throws IOException {
assertFile("test.java").sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
}

@Test
void removeModuleImports() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java {",
" target file('test.java')",
" forbidModuleImports()",
" }",
"}");

setFile("test.java").toResource("java/forbidmoduleimports/JavaCodeModuleImportsUnformatted.test");
gradleRunner().withArguments("spotlessApply").buildAndFail();
assertFile("test.java").sameAsResource("java/forbidmoduleimports/JavaCodeModuleImportsFormatted.test");
}

/**
* Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
* {@code targetExcludeContentPattern} both being not {@code null}.
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Fixed
- palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685))
### Added
- `<forbidModuleImports>` API for java ([#2679](https://github.com/diffplug/spotless/issues/2679))

## [3.0.0] - 2025-09-24
### Changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.java;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.java.ForbidModuleImportsStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class ForbidModuleImports implements FormatterStepFactory {
@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
return ForbidModuleImportsStep.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void addForbidWildcardImports(ForbidWildcardImports forbidWildcardImports
addStepFactory(forbidWildcardImports);
}

public void addForbidModuleImports(ForbidModuleImports forbidModuleImports) {
addStepFactory(forbidModuleImports);
}

public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
addStepFactory(formatAnnotations);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.java;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

class ForbidModuleImportsStepTest extends MavenIntegrationHarness {

@Test
void testForbidModuleImports() throws Exception {
writePomWithJavaSteps("<forbidModuleImports/>");

String path = "src/main/java/test.java";
setFile(path).toResource("java/forbidmoduleimports/JavaCodeModuleImportsUnformatted.test");
var selfie = expectSelfieErrorMsg(mavenRunner().withArguments("spotless:apply").runHasError());
assertFile(path).sameAsResource("java/forbidmoduleimports/JavaCodeModuleImportsUnformatted.test");
selfie.toBe("""
Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:VERSION:apply (default-cli) on project spotless-maven-plugin-tests: There were 2 lint error(s), they must be fixed or suppressed.
src/main/java/test.java:L1 forbidModuleImports(import module java.base;) Do not use module imports - replace with specific class imports as 'spotlessApply' cannot auto-fix this
src/main/java/test.java:L2 forbidModuleImports(import module java.sql;) Do not use module imports - replace with specific class imports as 'spotlessApply' cannot auto-fix this
Resolve these lints or suppress with `<lintSuppressions>`
""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import module java.base;
import module java.sql;

public class Test {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import module java.base;
import module java.sql;

public class Test {}