Skip to content

Commit 9c74556

Browse files
sergii-melnykSergii MelnykMBoegers
authored
Support Checkstyle UnusedImports (#5389)
* Support Checkstyle UnusedImports * add additional test case --------- Co-authored-by: Sergii Melnyk <smelnik@playtika.com> Co-authored-by: Merlin Bögershausen <merlin.boegershausen@rwth-aachen.de>
1 parent 8892230 commit 9c74556

4 files changed

Lines changed: 109 additions & 2 deletions

File tree

rewrite-java/src/main/java/org/openrewrite/java/style/Checkstyle.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ private Checkstyle() {
5454
operatorWrapStyle(),
5555
typecastParenPadStyle(),
5656
unnecessaryParentheses(),
57-
customImportOrderStyle()
57+
customImportOrderStyle(),
58+
unusedImportsStyle()
5859
));
5960
}
6061

@@ -155,4 +156,8 @@ public static CustomImportOrderStyle customImportOrderStyle() {
155156
new CustomImportOrderStyle.GroupWithDepth(CustomImportOrderStyle.CustomImportOrderGroup.THIRD_PARTY_PACKAGE, null)),
156157
true, false, "^$", "^(java|javax)\\.", ".*");
157158
}
159+
160+
private static UnusedImportsStyle unusedImportsStyle() {
161+
return new UnusedImportsStyle(false);
162+
}
158163
}

rewrite-java/src/main/java/org/openrewrite/java/style/CheckstyleConfigLoader.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public static Checkstyle loadCheckstyleConfig(InputStream checkstyleConf, Map<St
7878
operatorWrapStyle(conf),
7979
typecastParenPadStyle(conf),
8080
unnecessaryParentheses(conf),
81-
customImportOrderStyle(conf))
81+
customImportOrderStyle(conf),
82+
unusedImportsStyles(conf))
8283
.filter(Objects::nonNull)
8384
.flatMap(Set::stream)
8485
.collect(toSet()));
@@ -532,6 +533,16 @@ public static Checkstyle loadCheckstyleConfig(InputStream checkstyleConf, Map<St
532533
.collect(Collectors.toSet());
533534
}
534535

536+
private static @Nullable Set<UnusedImportsStyle> unusedImportsStyles(Map<String, List<Module>> conf) {
537+
List<Module> moduleList = conf.get("UnusedImports");
538+
if (moduleList == null) {
539+
return null;
540+
}
541+
return moduleList.stream()
542+
.map(module -> new UnusedImportsStyle(parseBoolean(module.properties.get("processJavadoc"))))
543+
.collect(toSet());
544+
}
545+
535546
protected static class Module {
536547
private final String name;
537548
private final Map<String, String> properties;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
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+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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 org.openrewrite.java.style;
17+
18+
import lombok.Value;
19+
import lombok.With;
20+
import org.openrewrite.java.JavaStyle;
21+
22+
@Value
23+
@With
24+
public class UnusedImportsStyle implements JavaStyle {
25+
26+
/**
27+
* Control whether to process Javadoc comments.
28+
*/
29+
boolean processJavadoc;
30+
}

rewrite-java/src/test/java/org/openrewrite/java/style/CheckstyleConfigLoaderTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,65 @@ void customImportOrderStyle() throws CheckstyleException {
363363
new CustomImportOrderStyle.GroupWithDepth(CustomImportOrderStyle.CustomImportOrderGroup.SPECIAL_IMPORTS, null),
364364
new CustomImportOrderStyle.GroupWithDepth(CustomImportOrderStyle.CustomImportOrderGroup.STATIC, null));
365365
}
366+
367+
@Test
368+
void unusedImportsWithJavadocProcessing() throws CheckstyleException {
369+
var checkstyle = loadCheckstyleConfig("""
370+
<!DOCTYPE module PUBLIC
371+
"-//Checkstyle//DTD Checkstyle Configuration 1.2//EN"
372+
"https://checkstyle.org/dtds/configuration_1_2.dtd">
373+
<module name="Checker">
374+
<module name="TreeWalker">
375+
<module name="UnusedImports">
376+
<property name="processJavadoc" value="true"/>
377+
</module>
378+
</module>
379+
</module>
380+
""", emptyMap());
381+
382+
assertThat(checkstyle.getStyles()).hasSize(1).allSatisfy(style -> {
383+
assertThat(style).isInstanceOf(UnusedImportsStyle.class);
384+
assertThat(((UnusedImportsStyle) style).isProcessJavadoc()).isTrue();
385+
});
386+
}
387+
388+
@Test
389+
void unusedImportsWithoutExpliciteJavadocProcessing() throws CheckstyleException {
390+
var checkstyle = loadCheckstyleConfig("""
391+
<!DOCTYPE module PUBLIC
392+
"-//Checkstyle//DTD Checkstyle Configuration 1.2//EN"
393+
"https://checkstyle.org/dtds/configuration_1_2.dtd">
394+
<module name="Checker">
395+
<module name="TreeWalker">
396+
<module name="UnusedImports">
397+
<property name="processJavadoc" value="false"/>
398+
</module>
399+
</module>
400+
</module>
401+
""", emptyMap());
402+
403+
assertThat(checkstyle.getStyles()).hasSize(1).allSatisfy(style -> {
404+
assertThat(style).isInstanceOf(UnusedImportsStyle.class);
405+
assertThat(((UnusedImportsStyle) style).isProcessJavadoc()).isFalse();
406+
});
407+
}
408+
409+
@Test
410+
void unusedImportsWithoutJavadocProcessing() throws CheckstyleException {
411+
var checkstyle = loadCheckstyleConfig("""
412+
<!DOCTYPE module PUBLIC
413+
"-//Checkstyle//DTD Checkstyle Configuration 1.2//EN"
414+
"https://checkstyle.org/dtds/configuration_1_2.dtd">
415+
<module name="Checker">
416+
<module name="TreeWalker">
417+
<module name="UnusedImports"/>
418+
</module>
419+
</module>
420+
""", emptyMap());
421+
422+
assertThat(checkstyle.getStyles()).hasSize(1).allSatisfy(style -> {
423+
assertThat(style).isInstanceOf(UnusedImportsStyle.class);
424+
assertThat(((UnusedImportsStyle) style).isProcessJavadoc()).isFalse();
425+
});
426+
}
366427
}

0 commit comments

Comments
 (0)