Skip to content

Commit ebc8940

Browse files
committed
Add Spring specific HideUtilityClassConstructor
Add a Spring variant of `HideUtilityClassConstructor` that will automatically bypass checks for `@SpringApplication` and `@Configuration` classes. Closes gh-74
1 parent f570ce4 commit ebc8940

File tree

8 files changed

+159
-1
lines changed

8 files changed

+159
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
* 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+
17+
package io.spring.javaformat.checkstyle.check;
18+
19+
import java.util.Collections;
20+
import java.util.LinkedHashSet;
21+
import java.util.Set;
22+
import java.util.stream.Collectors;
23+
24+
import com.puppycrawl.tools.checkstyle.api.DetailAST;
25+
import com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck;
26+
import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
27+
28+
/**
29+
* Extension to {@link HideUtilityClassConstructorCheck} that doesn't fail certain common
30+
* Spring patterns.
31+
*
32+
* @author Phillip Webb
33+
*/
34+
public class SpringHideUtilityClassConstructor extends HideUtilityClassConstructorCheck {
35+
36+
private static final Set<String> BYPASS_ANNOTATIONS;
37+
static {
38+
Set<String> annotations = new LinkedHashSet<>();
39+
annotations.add("org.springframework.context.annotation.Configuration");
40+
annotations.add("org.springframework.boot.autoconfigure.SpringBootApplication");
41+
annotations.add("org.springframework.boot.autoconfigure.EnableAutoConfiguration");
42+
Set<String> shortNames = annotations.stream()
43+
.map((name) -> name.substring(name.lastIndexOf(".") + 1))
44+
.collect(Collectors.toSet());
45+
annotations.addAll(shortNames);
46+
BYPASS_ANNOTATIONS = Collections.unmodifiableSet(annotations);
47+
}
48+
49+
@Override
50+
public void visitToken(DetailAST ast) {
51+
if (!isBypassed(ast)) {
52+
super.visitToken(ast);
53+
}
54+
}
55+
56+
private boolean isBypassed(DetailAST ast) {
57+
for (String bypassAnnotation : BYPASS_ANNOTATIONS) {
58+
if (AnnotationUtil.containsAnnotation(ast, bypassAnnotation)) {
59+
return true;
60+
}
61+
}
62+
return false;
63+
}
64+
65+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<!-- Class Design -->
4343
<module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck" />
4444
<module name="com.puppycrawl.tools.checkstyle.checks.design.InterfaceIsTypeCheck" />
45-
<module name="com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck" />
45+
<module name="io.spring.javaformat.checkstyle.check.SpringHideUtilityClassConstructor" />
4646
<module name="com.puppycrawl.tools.checkstyle.checks.design.MutableExceptionCheck" />
4747
<module name="com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck" />
4848
<module name="com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+HideUtilityClassConstructorInvalid.java:22:1: hide.utility.class [SpringHideUtilityClassConstructor]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
18+
* Invlid utility class.
19+
*
20+
* @author Phillip Webb
21+
*/
22+
public class HideUtilityClassConstructorInvalid {
23+
24+
public static void main(String[] args) {
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import org.springframework.boot.autoconfigure.SpringBootApplication;
18+
19+
/**
20+
* Usually invalid utility class but OK because it's a Spring Application.
21+
*
22+
* @author Phillip Webb
23+
*/
24+
@SpringBootApplication
25+
public class HideUtilityClassConstructorSpringApplication {
26+
27+
public static void main(String[] args) {
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import org.springframework.context.annotation.Bean;
18+
import org.springframework.context.annotation.Configuration;
19+
20+
/**
21+
* Usually invalid utility class but OK because it's a Spring Configuration.
22+
*
23+
* @author Phillip Webb
24+
*/
25+
@Configuration
26+
public class HideUtilityClassConstructorSpringConfiguration {
27+
28+
@Bean
29+
public static String myBean() {
30+
return "foo";
31+
}
32+
33+
}

0 commit comments

Comments
 (0)