Skip to content

Commit

Permalink
Add Spring specific HideUtilityClassConstructor
Browse files Browse the repository at this point in the history
Add a Spring variant of `HideUtilityClassConstructor` that will
automatically bypass checks for `@SpringApplication` and
`@Configuration` classes.

Closes gh-74
  • Loading branch information
philwebb committed Mar 30, 2019
1 parent f570ce4 commit ebc8940
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 1 deletion.
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<!-- Class Design -->
<module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.InterfaceIsTypeCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck" />
<module name="io.spring.javaformat.checkstyle.check.SpringHideUtilityClassConstructor" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.MutableExceptionCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+HideUtilityClassConstructorInvalid.java:22:1: hide.utility.class [SpringHideUtilityClassConstructor]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
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) {
}

}
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) {
}

}
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";
}

}

0 comments on commit ebc8940

Please sign in to comment.