我在我的项目中配置了一个Checkstyle验证规则,禁止定义具有超过3个输入参数的类方法。该规则适用于我的类,但有时我必须扩展第三方类,这些类不遵守这一特定规则。

是否有可能指示Checkstyle,某个方法应该被无声地忽略?

顺便说一句,我最终得到了我自己的Checkstyle包装器:qulice.com(参见Java代码质量的严格控制)


当前回答

在http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter上查看supressionCommentFilter的使用。您需要将模块添加到checkstyle.xml中

<module name="SuppressionCommentFilter"/>

它是可配置的。因此,您可以在代码中添加注释来关闭检查样式(在不同级别上),然后通过在代码中使用注释来再次打开检查样式。如。

//CHECKSTYLE:OFF
public void someMethod(String arg1, String arg2, String arg3, String arg4) {
//CHECKSTYLE:ON

或者更好的是,使用下面的版本:

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

它允许你关闭特定代码行的特定检查:

//CHECKSTYLE.OFF: IllegalCatch - Much more readable than catching 7 exceptions
catch (Exception e)
//CHECKSTYLE.ON: IllegalCatch

*注意:你还需要添加FileContentsHolder:

<module name="FileContentsHolder"/>

另请参阅

<module name="SuppressionFilter">
    <property name="file" value="docs/suppressions.xml"/>
</module>

在同一页面的SuppressionFilter部分下,它允许您关闭模式匹配资源的单独检查。

如果你在checkstyle。xml中有:

<module name="ParameterNumber">
   <property name="id" value="maxParameterNumber"/>
   <property name="max" value="3"/>
   <property name="tokens" value="METHOD_DEF"/>
</module>

你可以在你的抑制xml文件中关闭它:

<suppress id="maxParameterNumber" files="YourCode.java"/>

Checkstyle 5.7中现在可用的另一种方法是通过@SuppressWarnings java注释来抑制违规。要做到这一点,你需要在配置文件中添加两个新模块(SuppressWarningsFilter和SuppressWarningsHolder):

<module name="Checker">
   ...
   <module name="SuppressWarningsFilter" />
   <module name="TreeWalker">
       ...
       <module name="SuppressWarningsHolder" />
   </module>
</module> 

然后,在你的代码中,你可以做以下事情:

@SuppressWarnings("checkstyle:methodlength")
public void someLongMethod() throws Exception {

或者,对于多重抑制:

@SuppressWarnings({"checkstyle:executablestatementcount", "checkstyle:methodlength"})
public void someLongMethod() throws Exception {

注意:“checkstyle:”前缀是可选的(但推荐使用)。根据文档,参数名必须全小写,但实践表明,任何情况下工作。

其他回答

你也可以用这些特殊的注释来包围你想要禁用特定警告的代码:

// CHECKSTYLE:DISABLE:<CheckName>
<Your code goes here>
// CHECKSTYLE:ENABLE:<CheckName>

例:// CHECKSTYLE:DISABLE:ParameterNumberCheck

您可以在这里找到支持检查的完整列表(参见直接已知子类)。

如果你从qulice mvn插件(https://github.com/teamed/qulice)使用checkstyle,你可以使用以下抑制:

// @checkstyle <Rulename> (N lines)
... code with violation(s)

or


/**
 * ...
 * @checkstyle <Rulename> (N lines)
 * ...
 */
 ... code with violation(s)

每个引用SuppressWarningsFilter的答案都缺少一个重要的细节。如果在checkstyle-config.xml中定义了全小写的id,则只能使用它。如果不是,则必须使用原来的模块名称。

例如,如果在我的checkstyle-config.xml中有:

<module name="NoWhitespaceBefore"/>

我不能使用:

@SuppressWarnings({"nowhitespacebefore"})

然而,我必须使用:

@SuppressWarnings({"NoWhitespaceBefore"})

为了让第一个语法工作,checkstyle-config.xml应该有:

<module name="NoWhitespaceBefore">
  <property name="id" value="nowhitespacebefore"/>
</module>

这对我来说是有效的,至少在CheckStyle 6.17版本中是这样。

如果你更喜欢使用注释来选择性地屏蔽规则,现在可以使用@SuppressWarnings注释,从Checkstyle 5.7开始(Checkstyle Maven Plugin 2.12+支持)。

首先,在checkstyle.xml中,将SuppressWarningsHolder模块添加到TreeWalker中:

<module name="TreeWalker">
    <!-- Make the @SuppressWarnings annotations available to Checkstyle -->
    <module name="SuppressWarningsHolder" />
</module>

接下来,在那里启用SuppressWarningsFilter(作为TreeWalker的兄弟):

<!-- Filter out Checkstyle warnings that have been suppressed with the @SuppressWarnings annotation -->
<module name="SuppressWarningsFilter" />

<module name="TreeWalker">
...

现在你可以注释你想要排除在Checkstyle规则之外的方法:

@SuppressWarnings("checkstyle:methodlength")
@Override
public boolean equals(Object obj) {
    // very long auto-generated equals() method
}

@SuppressWarnings参数中的checkstyle:前缀是可选的,但我喜欢用它来提醒这个警告来自哪里。规则名称必须为小写。

最后,如果你正在使用Eclipse,它会抱怨参数对它是未知的:

不支持@SuppressWarnings(“checkstyle: methodlength”)

如果你愿意,你可以在首选项中禁用这个Eclipse警告:

Preferences:
  Java
  --> Compiler
  --> Errors/Warnings
  --> Annotations
  --> Unhandled token in '@SuppressWarnings': set to 'Ignore'
<module name="Checker">
    <module name="SuppressionCommentFilter"/>
    <module name="TreeWalker">
        <module name="FileContentsHolder"/>
    </module>
</module>

使用实例配置一个过滤器,在包含BEGIN GENERATED CODE的注释和包含END GENERATED CODE的注释之间屏蔽审计事件:

<module name="SuppressionCommentFilter">
  <property name="offCommentFormat" value="BEGIN GENERATED CODE"/>
  <property name="onCommentFormat" value="END GENERATED CODE"/>
</module>

//BEGIN GENERATED CODE
@Override
public boolean equals(Object obj) { ... } // No violation events will be reported

@Override
public int hashCode() { ... } // No violation events will be reported
//END GENERATED CODE

查看更多