-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spring specific HideUtilityClassConstructor
Add a Spring variant of `HideUtilityClassConstructor` that will automatically bypass checks for `@SpringApplication` and `@Configuration` classes. Closes gh-74
- Loading branch information
Showing
8 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...rc/main/java/io/spring/javaformat/checkstyle/check/SpringHideUtilityClassConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* | ||
* 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 io.spring.javaformat.checkstyle.check; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck; | ||
import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; | ||
|
||
/** | ||
* Extension to {@link HideUtilityClassConstructorCheck} that doesn't fail certain common | ||
* Spring patterns. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
public class SpringHideUtilityClassConstructor extends HideUtilityClassConstructorCheck { | ||
|
||
private static final Set<String> BYPASS_ANNOTATIONS; | ||
static { | ||
Set<String> annotations = new LinkedHashSet<>(); | ||
annotations.add("org.springframework.context.annotation.Configuration"); | ||
annotations.add("org.springframework.boot.autoconfigure.SpringBootApplication"); | ||
annotations.add("org.springframework.boot.autoconfigure.EnableAutoConfiguration"); | ||
Set<String> shortNames = annotations.stream() | ||
.map((name) -> name.substring(name.lastIndexOf(".") + 1)) | ||
.collect(Collectors.toSet()); | ||
annotations.addAll(shortNames); | ||
BYPASS_ANNOTATIONS = Collections.unmodifiableSet(annotations); | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
if (!isBypassed(ast)) { | ||
super.visitToken(ast); | ||
} | ||
} | ||
|
||
private boolean isBypassed(DetailAST ast) { | ||
for (String bypassAnnotation : BYPASS_ANNOTATIONS) { | ||
if (AnnotationUtil.containsAnnotation(ast, bypassAnnotation)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ing-javaformat-checkstyle/src/test/resources/check/HideUtilityClassConstructorInvalid.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
+HideUtilityClassConstructorInvalid.java:22:1: hide.utility.class [SpringHideUtilityClassConstructor] |
1 change: 1 addition & 0 deletions
1
...rmat-checkstyle/src/test/resources/check/HideUtilityClassConstructorSpringApplication.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
+0 errors |
1 change: 1 addition & 0 deletions
1
...at-checkstyle/src/test/resources/check/HideUtilityClassConstructorSpringConfiguration.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
+0 errors |
27 changes: 27 additions & 0 deletions
27
...g-javaformat-checkstyle/src/test/resources/source/HideUtilityClassConstructorInvalid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/** | ||
* Invlid utility class. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
public class HideUtilityClassConstructorInvalid { | ||
|
||
public static void main(String[] args) { | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...at-checkstyle/src/test/resources/source/HideUtilityClassConstructorSpringApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Usually invalid utility class but OK because it's a Spring Application. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
@SpringBootApplication | ||
public class HideUtilityClassConstructorSpringApplication { | ||
|
||
public static void main(String[] args) { | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...-checkstyle/src/test/resources/source/HideUtilityClassConstructorSpringConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Usually invalid utility class but OK because it's a Spring Configuration. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
@Configuration | ||
public class HideUtilityClassConstructorSpringConfiguration { | ||
|
||
@Bean | ||
public static String myBean() { | ||
return "foo"; | ||
} | ||
|
||
} |