14

我有一个看起来像这样的 checkstyle.xml:

<module name="Checker">
    ....

    <module name="SuppressionCommentFilter">
        <property name="offCommentFormat" value="CSOFF\: ([\w\|]+)"/>
        <property name="onCommentFormat" value="CSON\: ([\w\|]+)"/>
        <property name="checkFormat" value="$1"/>
    </module>

    <module name="TreeWalker">
        <module name="LineLength">
            <property name="max" value="200"/>
        </module>
        ....
    </module>
</module>

在我的一个课程中,我有一行超过 200 个字符,并在其周围放置以下内容:

// CSOFF: LineLength
...
// CSON: LineLength

然而,作为 checkstyle 的一部分,有问题的行不会被忽略。

我在 pom.xml 中指定了以下内容:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
            </configuration>
        </plugin>
    </plugins>
</build>

并执行这个:

mvn checkstyle:checkstyle
4

2 回答 2

29

您是否按照文档配置了 FileContentsHolder ?

<module name="TreeWalker">
    ...
    <module name="FileContentsHolder"/>
    ...
</module>
于 2011-04-23T14:14:10.677 回答
2

这对我最近也不起作用,但自checkstyle 8.2以来接受的答案已经过时:

删除 FileContentsHolder 模块,因为 FileContents 对象可用于 TreeWalkerAudit 事件中 TreeWalker 上的过滤器。

但是8.6 版添加了SuppressWithPlainTextCommentFilter

新的 Checker 过滤器 SuppressWithPlainTextCommentFilter 类似于 Treewalker 的 SuppressionCommentFilter。

我没有使用SuppressionCommentFilter上面的方法SuppressWithPlainTextCommentFilter,一切都开始工作了。

例子:

  <module name="TreeWalker">
    ...
  </module>
  <module name="SuppressWithPlainTextCommentFilter">
    <property name="offCommentFormat" value="CSOFF: ALL"/>
    <property name="onCommentFormat" value="CSON: ALL"/>
  </module>
  <module name="SuppressWithPlainTextCommentFilter">
    <property name="offCommentFormat" value="CSOFF\: ([\w\|]+)"/>
    <property name="onCommentFormat" value="CSON\: ([\w\|]+)"/>
    <property name="checkFormat" value="$1"/>
  </module>

现在我可以做

public static final int lowerCaseConstant; // CSOFF: ConstantNameCheck
public final static int MultipleERRORS;; // CSOFF: ALL
于 2021-06-29T04:07:53.243 回答