Skip to content

Commit c03c9ea

Browse files
committed
Make it easier to exclude more static imports
Add `SpringAvoidStaticImportCheck` so that it's easier to add more static import exclusions without needing to list them all. Closes gh-201
1 parent ac53cdf commit c03c9ea

File tree

4 files changed

+111
-52
lines changed

4 files changed

+111
-52
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringChecks.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import java.io.File;
2020
import java.util.Arrays;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.LinkedHashSet;
2425
import java.util.Properties;
2526
import java.util.Set;
2627
import java.util.SortedSet;
2728
import java.util.TreeSet;
29+
import java.util.stream.Collectors;
2830

2931
import com.puppycrawl.tools.checkstyle.Checker;
3032
import com.puppycrawl.tools.checkstyle.DefaultContext;
@@ -61,6 +63,8 @@ public class SpringChecks extends AbstractFileSetCheck implements ExternalResour
6163

6264
private String headerFile;
6365

66+
private Set<String> avoidStaticImportExcludes = Collections.emptySet();
67+
6468
private String projectRootPackage = SpringImportOrderCheck.DEFAULT_PROJECT_ROOT_PACKAGE;
6569

6670
private Set<String> excludes;
@@ -94,6 +98,8 @@ public void finishLocalSetup() {
9498
put(properties, "headerCopyrightPattern", this.headerCopyrightPattern);
9599
put(properties, "headerFile", this.headerFile);
96100
put(properties, "projectRootPackage", this.projectRootPackage);
101+
put(properties, "avoidStaticImportExcludes",
102+
this.avoidStaticImportExcludes.stream().collect(Collectors.joining(",")));
97103
this.checks = new SpringConfigurationLoader(context, moduleFactory).load(new PropertiesExpander(properties));
98104
}
99105

@@ -153,6 +159,10 @@ public void setHeaderFile(String headerFile) {
153159
this.headerFile = headerFile;
154160
}
155161

162+
public void setAvoidStaticImportExcludes(String[] avoidStaticImportExcludes) {
163+
this.avoidStaticImportExcludes = new LinkedHashSet<>(Arrays.asList(avoidStaticImportExcludes));
164+
}
165+
156166
public void setProjectRootPackage(String projectRootPackage) {
157167
this.projectRootPackage = projectRootPackage;
158168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2017-2019 the original author or authors.
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+
* https://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+
17+
package io.spring.javaformat.checkstyle.check;
18+
19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
23+
24+
import com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck;
25+
import com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck;
26+
27+
/**
28+
* Spring variant of {@link AvoidStarImportCheck}.
29+
*/
30+
public class SpringAvoidStaticImportCheck extends AvoidStaticImportCheck {
31+
32+
private static final Set<String> ALWAYS_EXCLUDED;
33+
static {
34+
Set<String> excludes = new LinkedHashSet<>();
35+
excludes.add("io.restassured.RestAssured.*");
36+
excludes.add("org.assertj.core.api.Assertions.*");
37+
excludes.add("org.assertj.core.api.Assumptions.*");
38+
excludes.add("org.assertj.core.api.HamcrestCondition.*");
39+
excludes.add("org.awaitility.Awaitility.*");
40+
excludes.add("org.hamcrest.CoreMatchers.*");
41+
excludes.add("org.hamcrest.Matchers.*");
42+
excludes.add("org.junit.Assert.*");
43+
excludes.add("org.junit.Assume.*");
44+
excludes.add("org.junit.internal.matchers.ThrowableMessageMatcher.*");
45+
excludes.add("org.junit.jupiter.api.Assertions.*");
46+
excludes.add("org.junit.jupiter.api.Assumptions.*");
47+
excludes.add("org.junit.jupiter.api.Assertions.*");
48+
excludes.add("org.mockito.ArgumentMatchers.*");
49+
excludes.add("org.mockito.BDDMockito.*");
50+
excludes.add("org.mockito.Matchers.*");
51+
excludes.add("org.mockito.Mockito.*");
52+
excludes.add("org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.*");
53+
excludes.add("org.springframework.boot.configurationprocessor.TestCompiler.*");
54+
excludes.add("org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.*");
55+
excludes.add("org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo");
56+
excludes.add("org.springframework.restdocs.headers.HeaderDocumentation.*");
57+
excludes.add("org.springframework.restdocs.hypermedia.HypermediaDocumentation.*");
58+
excludes.add("org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*");
59+
excludes.add("org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*");
60+
excludes.add("org.springframework.restdocs.operation.preprocess.Preprocessors.*");
61+
excludes.add("org.springframework.restdocs.payload.PayloadDocumentation.*");
62+
excludes.add("org.springframework.restdocs.request.RequestDocumentation.*");
63+
excludes.add("org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.*");
64+
excludes.add("org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.*");
65+
excludes.add("org.springframework.restdocs.snippet.Attributes.*");
66+
excludes.add("org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.*");
67+
excludes.add("org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*");
68+
excludes.add("org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*");
69+
excludes.add("org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*");
70+
excludes.add("org.springframework.test.web.client.ExpectedCount.*");
71+
excludes.add("org.springframework.test.web.client.match.MockRestRequestMatchers.*");
72+
excludes.add("org.springframework.test.web.client.response.MockRestResponseCreators.*");
73+
excludes.add("org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*");
74+
excludes.add("org.springframework.test.web.servlet.result.MockMvcResultHandlers.*");
75+
excludes.add("org.springframework.test.web.servlet.result.MockMvcResultMatchers.*");
76+
excludes.add("org.springframework.test.web.servlet.setup.MockMvcBuilders.*");
77+
excludes.add("org.springframework.web.reactive.function.BodyInserters.*");
78+
excludes.add("org.springframework.web.reactive.function.server.RequestPredicates.*");
79+
excludes.add("org.springframework.web.reactive.function.server.RouterFunctions.*");
80+
excludes.add("org.springframework.ws.test.client.RequestMatchers.*");
81+
excludes.add("org.springframework.ws.test.client.ResponseCreators.*");
82+
ALWAYS_EXCLUDED = Collections.unmodifiableSet(excludes);
83+
}
84+
85+
public SpringAvoidStaticImportCheck() {
86+
setExcludes(ALWAYS_EXCLUDED.toArray(new String[0]));
87+
}
88+
89+
@Override
90+
public void setExcludes(String... excludes) {
91+
Set<String> merged = new LinkedHashSet<>(ALWAYS_EXCLUDED);
92+
merged.addAll(Arrays.asList(excludes));
93+
super.setExcludes(merged.toArray(new String[0]));
94+
}
95+
96+
}

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -73,56 +73,6 @@
7373

7474
<!-- Imports -->
7575
<module name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck" />
76-
<module name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck">
77-
<property name="excludes" value="
78-
io.restassured.RestAssured.*,
79-
org.assertj.core.api.Assertions.*,
80-
org.assertj.core.api.Assumptions.*,
81-
org.assertj.core.api.HamcrestCondition.*,
82-
org.awaitility.Awaitility.*,
83-
org.hamcrest.CoreMatchers.*,
84-
org.hamcrest.Matchers.*,
85-
org.junit.Assert.*,
86-
org.junit.Assume.*,
87-
org.junit.internal.matchers.ThrowableMessageMatcher.*,
88-
org.junit.jupiter.api.Assertions.*,
89-
org.junit.jupiter.api.Assumptions.*,
90-
org.junit.jupiter.api.Assertions.*,
91-
org.mockito.ArgumentMatchers.*,
92-
org.mockito.BDDMockito.*,
93-
org.mockito.Matchers.*,
94-
org.mockito.Mockito.*,
95-
org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.*,
96-
org.springframework.boot.configurationprocessor.TestCompiler.*,
97-
org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.*,
98-
org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo,
99-
org.springframework.restdocs.headers.HeaderDocumentation.*,
100-
org.springframework.restdocs.hypermedia.HypermediaDocumentation.*,
101-
org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*,
102-
org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*,
103-
org.springframework.restdocs.operation.preprocess.Preprocessors.*,
104-
org.springframework.restdocs.payload.PayloadDocumentation.*,
105-
org.springframework.restdocs.request.RequestDocumentation.*,
106-
org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.*,
107-
org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.*,
108-
org.springframework.restdocs.snippet.Attributes.*,
109-
org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.*,
110-
org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*,
111-
org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*,
112-
org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*,
113-
org.springframework.test.web.client.ExpectedCount.*,
114-
org.springframework.test.web.client.match.MockRestRequestMatchers.*,
115-
org.springframework.test.web.client.response.MockRestResponseCreators.*,
116-
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*,
117-
org.springframework.test.web.servlet.result.MockMvcResultHandlers.*,
118-
org.springframework.test.web.servlet.result.MockMvcResultMatchers.*,
119-
org.springframework.test.web.servlet.setup.MockMvcBuilders.*,
120-
org.springframework.web.reactive.function.BodyInserters.*,
121-
org.springframework.web.reactive.function.server.RequestPredicates.*,
122-
org.springframework.web.reactive.function.server.RouterFunctions.*,
123-
org.springframework.ws.test.client.RequestMatchers.*,
124-
org.springframework.ws.test.client.ResponseCreators.*" />
125-
</module>
12676
<module name="com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck" />
12777
<module name="com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck">
12878
<property name="processJavadoc" value="true" />
@@ -208,6 +158,9 @@
208158
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck" />
209159

210160
<!-- Spring Conventions -->
161+
<module name="io.spring.javaformat.checkstyle.check.SpringAvoidStaticImportCheck" >
162+
<property name="excludes" value="${avoidStaticImportExcludes}"/>
163+
</module>
211164
<module name="io.spring.javaformat.checkstyle.check.SpringLambdaCheck" />
212165
<module name="io.spring.javaformat.checkstyle.check.SpringTernaryCheck" />
213166
<module name="io.spring.javaformat.checkstyle.check.SpringCatchCheck" />

spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ public void loadShouldLoadChecks() {
5353
}
5454

5555
@Test
56-
@SuppressWarnings({ "unchecked", "rawtypes" })
5756
public void loadWithExcludeShouldExcludeChecks() {
5857
Set<String> excludes = Collections
59-
.singleton("com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck");
58+
.singleton("com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck");
6059
Collection<FileSetCheck> checks = load(excludes);
6160
assertThat(checks).hasSize(3);
6261
TreeWalker treeWalker = (TreeWalker) checks.toArray()[2];
@@ -81,6 +80,7 @@ private PropertyResolver getPropertyResolver() {
8180
properties.put("headerFile", "");
8281
properties.put("headerCopyrightPattern", SpringHeaderCheck.DEFAULT_HEADER_COPYRIGHT_PATTERN);
8382
properties.put("projectRootPackage", SpringImportOrderCheck.DEFAULT_PROJECT_ROOT_PACKAGE);
83+
properties.put("avoidStaticImportExcludes", "");
8484
return new PropertiesExpander(properties);
8585
}
8686

0 commit comments

Comments
 (0)