我们可以根据 class 类型在 checkstyle 中指定何时应用 EmptyLineSeparator 吗?

Can we specify when EmptyLineSeparator is apply according to class type in checkstyle?

我正在为我的 java 项目使用文件 google_checks.xml 的 checkstyle(经过修改以获得 4 spaces 的缩进)。 当我扫描时,每个 Java 接口内的 METHOD_DEF 令牌都有 EmptyLineSeparator

因为我在接口中的方法没有大括号:

List<User> getAll ();
User findById (int userId);
// [...]

我的 linter 删除了行之间的空白 space,但 checkstyle 要求它们。

有没有办法指定哪个文件受检查规则影响,或者接口方法是否有标记?

我知道我可以在 Intellij 中配置它,但我正在寻找一种方法让团队成员导入项目并且不需要任何额外的配置。 谢谢

这可以通过 SuppressionFilter. You can add a separate suppression file or just add it to your google_checks.xml. A good example can be found in Checkstyle's own suppression file at https://github.com/checkstyle/checkstyle/blob/master/config/suppressions.xml 来实现。

您的模块应如下所示:


  <suppress checks="EmptyLineSeparator" files=".*YourFilenameHere\.java"/>

执行此操作的最佳方法是使用 SuppressionXpathFilter

抑制 XPath 的此类违规行为

您需要创建以下文件(或通过 xpath 文件向现有抑制添加条目)。它将抑制此检查对接口中所有方法定义的违规行为。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suppressions PUBLIC
    "-//Checkstyle//DTD SuppressionXpathFilter Experimental Configuration 1.2//EN"
    "https://checkstyle.org/dtds/suppressions_1_2_xpath_experimental.dtd">
<suppressions>
<suppress-xpath
       checks="EmptyLineSeparatorCheck"
       query="//INTERFACE_DEF/OBJBLOCK/METHOD_DEF"/>
</suppressions>

您的 checkstyle 配置文件应该扩展

<!-- should be under TreeWalker -->
<module name="SuppressionXpathFilter">
    <property name="file" value="/path/to/suppression-xpath.xml" />
    <property name="optional" value="false"/>
</module>