sda*_*are 8 java checkstyle lombok
在编译以下使用Lombok自动生成getter和setter的类时,Checkstyle会抛出一个编译错误:
实用程序类不应具有公共或默认构造函数
@Getter
@Setter
public class foo {
private String type;
private int value;
}
Run Code Online (Sandbox Code Playgroud)
当Checkstyle不遵循checkstyle文档中指定的实用程序类定义时,为什么Checkstyle将上面的类分类为实用程序类?即仅包含静态方法或字段的类.checkstyle是解析默认的源文本文件还是lombok生成的源文件?
对于 checkstyle 有一个很好的XPathSuppressionFilter。使用它
添加到您的 checkstyle.xml 文件
<!-- externalize the ignored/suppressed checks -->
<module name="SuppressionFilter">
<property name="file" value="./checkstyle-suppressions.xml" />
<property name="optional" value="false" />
</module>
Run Code Online (Sandbox Code Playgroud)
并在 checkstyle-suppressions.xml 中
<!-- disable checks against lombok annotations -->
<suppress-xpath checks="HideUtilityClassConstructor" query="//CLASS_DEF[.//ANNOTATION/IDENT[@text='UtilityClass']]"/>
<suppress-xpath checks="HideUtilityClassConstructor" query="//CLASS_DEF[.//ANNOTATION/IDENT[@text='Getter']]"/>
<suppress-xpath checks="HideUtilityClassConstructor" query="//CLASS_DEF[.//ANNOTATION/IDENT[@text='Setter']]"/>
Run Code Online (Sandbox Code Playgroud)
Checkstyle适用于源代码,它没有看到lombok会生成字节码,所以它看到一个只有两个私有字段的类,它假定你有一个实用程序类.如果private是checkstyle,实用程序类应该有一个构造函数,但是你可能不希望这样(你不能创建这个类的实例),所以你需要的是HideUtilityClassConstructor从checkstyle规则列表中删除,或者添加(请参阅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;
}
Run Code Online (Sandbox Code Playgroud)
如果注释不适合您(由于您正在使用的 checkstyle 版本),您可以使用
// CHECKSTYLE:SUPPRESS:HideUtilityClassConstructor
@Getter
@Setter
public class Foo {
private String type;
private int value;
}
// CHECKSTYLE:UNSUPPRESS:HideUtilityClassConstructor
Run Code Online (Sandbox Code Playgroud)
反而。
| 归档时间: |
|
| 查看次数: |
5537 次 |
| 最近记录: |