gpt4 book ai didi

java - 使用 Lombok 时的 Checkstyle 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:16 25 4
gpt4 key购买 nike

在编译以下使用 Lombok 自动生成 getter 和 setter 的类时,Checkstyle 会抛出一个编译错误:

Utility classes should not have a public or default constructor

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

当 Checkstyle 不遵循 checkstyle's documentation 中指定的实用程序类定义时,为什么 Checkstyle 将上述类归类为实用程序类? ?即只包含静态方法或字段的类。checkstyle 解析的是默认源文本文件还是lombok 生成的源文件?

最佳答案

Checkstyle 处理源代码,它看不到 lombok 会生成字节码,所以它看到一个只有两个私有(private)字段的类,它假定您有一个实用程序类。一个实用程序类应该有一个 private 构造函数以应对该检查样式,但您可能不希望这样(您将无法创建此类的实例),所以您需要的是从 checkstyle 规则列表中删除 HideUtilityClassConstructor,或添加(参见 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;
}

关于java - 使用 Lombok 时的 Checkstyle 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com