checkstyle error when analyzing java code

一笑奈何 提交于 2020-01-17 08:32:47

问题


i got this error when launching checkstyle analysis in a spring boot app :

SpringBootBackend.java:7:1  error: Les classes utilitaires ne doivent pas avoir de constructeur par défaut ou public.

Code:

  public class SpringBootBackend{

public static void main(String[] args) {

    SpringApplication.run(SpringBootBackend.class, args);
   }
}

Any help?


回答1:


Classes which contain only static methods are considered "utility classes" by this check. Such classes should have only private constructors, so that they are not accidentally instantiated. They should also be final.

So, you can add a constructor like this:

private SpringBootBackend() {}

and possibly declare the class final, and the error should be gone.




回答2:


Create a configuration file that suppress checks on files, checkstyle-suppressions.xml

 <?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
        "-//Puppy Crawl//DTD Suppressions 1.0//EN"
        "http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">

<suppressions>
    <suppress files="SpringBootApplication.java" checks="HideUtilityClassConstructor" />
</suppressions>

In maven-checkstyle-plugin plugin configuration, add location of checkstyle-suppressions.xml

    <configuration>
        <configLocation>quality/checkstyle.xml</configLocation>
        <suppressionsLocation>quality/checkstyle-suppressions.xml</suppressionsLocation>



回答3:


While the previous answer is technically correct from the checkstyle perspective, it won't work in this case because Spring Boot will try to create an instance of this class and it will fail because of the private constructor.

In this case, I decided to suppress this violation with the annotation:

@SuppressWarnings("checkstyle:hideutilityclassconstructor")
public static void main(String[] args) {


来源:https://stackoverflow.com/questions/44199634/checkstyle-error-when-analyzing-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!