问题

我在我的项目中配置了acheckstyle验证规则,禁止使用3个以上的输入参数定义类方法。该规则适用于精细的formyclasses,但有时我必须扩展不遵守此特定规则的第三方类。

是否有可能指示"checkstyle"某种方法应该被默默忽略?

顺便说一句,我最终得到了自己的checkstyle包装:qulice.com(见Strict Control of Java Code Quality)


#1 热门回答(224 赞)

查看supressionCommentFilter athttp://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter的使用情况。你需要将模块添加到checkstyle.xml

<module name="SuppressionCommentFilter"/>

它是可配置的。因此,你可以在代码中添加注释以关闭checkstyle(在各个级别),然后通过在代码中使用注释再次返回。例如。

//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中可用的另一种方法是通过8784858216java注释来抑制违规。为此,你需要在配置文件中使用新模块(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:"前缀是可选的(但建议使用),参数name必须全部小写


#2 热门回答(49 赞)

如果你更喜欢使用注释来选择性地使规则静音,现在可以使用@SuppressWarnings注释,从Checkstyle 5.7开始(并由Checkstyle Maven插件2.12支持)。

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

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

接下来,启用3885846723(作为兄弟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
}

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

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

不支持的@SuppressWarnings("checkstyle:methodlength")

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

Preferences:
  Java
  --> Compiler
  --> Errors/Warnings
  --> Annotations
  --> Unhandled token in '@SuppressWarnings': set to 'Ignore'

#3 热门回答(30 赞)

同样有效的是使用个别注释来抑制审计事件的SuppressWithNearbyCommentFilter

例如

// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
public void onClick(View view) { ... }

要配置过滤器以便CHECKSTYLE IGNORE检查FOR NEXT var LINES,可以避免触发对当前行和下一行变量的给定检查的任何审计(总共为var 1行):

<module name="SuppressWithNearbyCommentFilter">
    <property name="commentFormat" value="CHECKSTYLE IGNORE (\w+) FOR NEXT (\d+) LINES"/>
    <property name="checkFormat" value="$1"/>
    <property name="influenceFormat" value="$2"/>
</module>

http://checkstyle.sourceforge.net/config.html


原文链接