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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With