2

I want to suppress 'HideUtilityClassConstructor' rule the for classes that have main method. That look like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }
}
  • Since all checkstyle rules are located in the common library - it should be a generic skip for any module that uses those rules.
  • Name of the main class can be different (the skip should be applied for all classes that have public static main method)
  • The change should be located in checkstyle common xml files (not in main class, like using @SupressWarnings or comments)

Is it possible to do it?

Yurii Bondarenko
  • 3,460
  • 6
  • 28
  • 47

1 Answers1

0

You can achieve this with SuppressionXpathFilter. For example, for your code you need to add this module to your config (in this example suppressions are in file suppressions.xml)

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
      "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
      "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name = "Checker">

    <module name="TreeWalker">
        <module name="SuppressionXpathFilter">
            <property name="file" value="suppressions.xml"/>
            <property name="optional" value="false"/>
        </module>
        <module name="HideUtilityClassConstructor"/>
    </module>
</module>

And content of suppressions.xml file is like this. Here are 2 suppressions, you can pick any of them. Second looks better to me since there can be less possible false-positives.

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionXpathFilter Experimental Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2_xpath_experimental.dtd">

<suppressions>
  <!--  Suppress for any classes that contain method named "main"  -->
  <suppress-xpath checks="HideUtilityClassConstructor"
  query="//CLASS_DEF[//METHOD_DEF[./IDENT[@text='main']]]"/>

  <!--  Suppress for any classes that are annotated with "SpringBootApplication"  -->
  <suppress-xpath checks="HideUtilityClassConstructor"
   query="//CLASS_DEF[//MODIFIERS/ANNOTATION[./IDENT[@text='SpringBootApplication']]]"/>

</suppressions>
strkk
  • 589
  • 3
  • 7