Skip to content

Commit a038fa1

Browse files
committed
Add step to forbid module imports
1 parent ffb7c59 commit a038fa1

File tree

11 files changed

+146
-0
lines changed

11 files changed

+146
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Fixed
1414
- 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))
15+
### Added
16+
- Add a `forbidModuleImports` API for java ([#2679](https://github.com/diffplug/spotless/issues/2679))
1517

1618
## [4.0.0] - 2025-09-24
1719
### Changes
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.java;
17+
18+
import com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.generic.ReplaceRegexStep;
20+
21+
/** Forbids any module import statements. */
22+
public final class ForbidModuleImportsStep {
23+
24+
/**
25+
* Matches lines like 'import module java.base;' or 'import module java.sql;'.
26+
*/
27+
private static final String REGEX = "(?m)^import module[^;\\n]*;\\R?";
28+
private static final String NAME = "forbidModuleImports";
29+
private static final String ERROR = "Do not use module imports - replace with specific class imports as 'spotlessApply' cannot auto-fix this";
30+
31+
private ForbidModuleImportsStep() {}
32+
33+
public static FormatterStep create() {
34+
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);
35+
}
36+
}

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Fixed
77
- 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))
8+
### Added
9+
- `forbidModuleImports()` API for java ([#2679](https://github.com/diffplug/spotless/issues/2679))
810

911
## [8.0.0] - 2025-09-24
1012
### Changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.diffplug.spotless.extra.java.EclipseJdtFormatterStep;
3737
import com.diffplug.spotless.generic.LicenseHeaderStep;
3838
import com.diffplug.spotless.java.CleanthatJavaStep;
39+
import com.diffplug.spotless.java.ForbidModuleImportsStep;
3940
import com.diffplug.spotless.java.ForbidWildcardImportsStep;
4041
import com.diffplug.spotless.java.FormatAnnotationsStep;
4142
import com.diffplug.spotless.java.GoogleJavaFormatStep;
@@ -162,6 +163,10 @@ public void forbidWildcardImports() {
162163
addStep(ForbidWildcardImportsStep.create());
163164
}
164165

166+
public void forbidModuleImports() {
167+
addStep(ForbidModuleImportsStep.create());
168+
}
169+
165170
/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
166171
public GoogleJavaFormatConfig googleJavaFormat() {
167172
return googleJavaFormat(GoogleJavaFormatStep.defaultVersion());

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ void removeWildCardImports() throws IOException {
101101
assertFile("test.java").sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
102102
}
103103

104+
@Test
105+
void removeModuleImports() throws IOException {
106+
setFile("build.gradle").toLines(
107+
"plugins {",
108+
" id 'com.diffplug.spotless'",
109+
"}",
110+
"repositories { mavenCentral() }",
111+
"",
112+
"spotless {",
113+
" java {",
114+
" target file('test.java')",
115+
" forbidModuleImports()",
116+
" }",
117+
"}");
118+
119+
setFile("test.java").toResource("java/forbidmoduleimports/JavaCodeModuleImportsUnformatted.test");
120+
gradleRunner().withArguments("spotlessApply").buildAndFail();
121+
assertFile("test.java").sameAsResource("java/forbidmoduleimports/JavaCodeModuleImportsFormatted.test");
122+
}
123+
104124
/**
105125
* Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
106126
* {@code targetExcludeContentPattern} both being not {@code null}.

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Fixed
77
- 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))
8+
### Added
9+
- `<forbidModuleImports>` API for java ([#2679](https://github.com/diffplug/spotless/issues/2679))
810

911
## [3.0.0] - 2025-09-24
1012
### Changes
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.java;
17+
18+
import com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.java.ForbidModuleImportsStep;
20+
import com.diffplug.spotless.maven.FormatterStepConfig;
21+
import com.diffplug.spotless.maven.FormatterStepFactory;
22+
23+
public class ForbidModuleImports implements FormatterStepFactory {
24+
@Override
25+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
26+
return ForbidModuleImportsStep.create();
27+
}
28+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public void addForbidWildcardImports(ForbidWildcardImports forbidWildcardImports
8080
addStepFactory(forbidWildcardImports);
8181
}
8282

83+
public void addForbidModuleImports(ForbidModuleImports forbidModuleImports) {
84+
addStepFactory(forbidModuleImports);
85+
}
86+
8387
public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
8488
addStepFactory(formatAnnotations);
8589
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.java;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
22+
class ForbidModuleImportsStepTest extends MavenIntegrationHarness {
23+
24+
@Test
25+
void testForbidModuleImports() throws Exception {
26+
writePomWithJavaSteps("<forbidModuleImports/>");
27+
28+
String path = "src/main/java/test.java";
29+
setFile(path).toResource("java/forbidmoduleimports/JavaCodeModuleImportsUnformatted.test");
30+
var selfie = expectSelfieErrorMsg(mavenRunner().withArguments("spotless:apply").runHasError());
31+
assertFile(path).sameAsResource("java/forbidmoduleimports/JavaCodeModuleImportsUnformatted.test");
32+
selfie.toBe("""
33+
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.
34+
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
35+
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
36+
Resolve these lints or suppress with `<lintSuppressions>`
37+
""");
38+
}
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import module java.base;
2+
import module java.sql;
3+
4+
public class Test {}

0 commit comments

Comments
 (0)