Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle errors while using Lombok

While compiling following class which uses Lombok for auto generating getters and setters, Checkstyle throws a compilation error:

Utility classes should not have a public or default constructor

@Getter
@Setter
public class foo {
    private String type;
    private int value;
}

Why does Checkstyle classify the above class as utility class, when it does not follow the utility class definition as specified in checkstyle's documentation? i.e. classes containing only static methods or fields. Is checkstyle parsing the default source text file or the lombok generated source file?

like image 931
sdare Avatar asked Apr 14 '15 12:04

sdare


1 Answers

Checkstyle works on the source code, it doesn't see that lombok will generate bytecode so it sees a class that has only two private fields it assumes you have an utility class. An utility class should have a private constructor in case of that checkstyle, but you probably don't want that (you wouldn't be able to create instance of this class), so what you need is either remove HideUtilityClassConstructor from list of checkstyle rules, or add (see http://checkstyle.sourceforge.net/config_annotation.html#SuppressWarnings#SuppressWarningsHolder) @SuppressWarnings("checkstyle:HideUtilityClassConstructor"):

@Getter
@Setter
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class foo {
    private String type;
    private int value;
}
like image 198
Krzysztof Krasoń Avatar answered Sep 28 '22 11:09

Krzysztof Krasoń