如何为特定代码行禁用特定的checkstyle规则?

yeg*_*256 174 java checkstyle

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

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

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

Chr*_*ght 276

Check out the use of the supressionCommentFilter at http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter. You'll need to add the module to your checkstyle.xml

<module name="SuppressionCommentFilter"/>
Run Code Online (Sandbox Code Playgroud)

and it's configurable. Thus you can add comments to your code to turn off checkstyle (at various levels) and then back on again through the use of comments in your code. E.g.

//CHECKSTYLE:OFF
public void someMethod(String arg1, String arg2, String arg3, String arg4) {
//CHECKSTYLE:ON
Run Code Online (Sandbox Code Playgroud)

Or even better, use this more tweaked version:

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

which allows you to turn off specific checks for specific lines of code:

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

*Note: you'll also have to add the FileContentsHolder:

<module name="FileContentsHolder"/>
Run Code Online (Sandbox Code Playgroud)

See also

<module name="SuppressionFilter">
    <property name="file" value="docs/suppressions.xml"/>
</module>
Run Code Online (Sandbox Code Playgroud)

under the SuppressionFilter section on that same page, which allows you to turn off individual checks for pattern matched resources.

So, if you have in your checkstyle.xml:

<module name="ParameterNumber">
   <property name="id" value="maxParameterNumber"/>
   <property name="max" value="3"/>
   <property name="tokens" value="METHOD_DEF"/>
</module>
Run Code Online (Sandbox Code Playgroud)

You can turn it off in your suppression xml file with:

<suppress id="maxParameterNumber" files="YourCode.java"/>
Run Code Online (Sandbox Code Playgroud)

Another method, now available in Checkstyle 5.7 is to suppress violations via the SuppressionFilter java annotation. To do this, you will need to new modules (SuppressWarningsFilter and SuppressWarningsHolder) in your configuration file:

<module name="Checker">
   ...
   <module name="SuppressWarningsFilter" />
   <module name="TreeWalker">
       ...
       <module name="SuppressWarningsHolder" />
   </module>
</module> 
Run Code Online (Sandbox Code Playgroud)

Then, within your code you can do the following:

@SuppressWarnings("checkstyle:methodlength")
public void someLongMethod() throws Exception {
Run Code Online (Sandbox Code Playgroud)

or, for multiple suppressions:

@SuppressWarnings({"checkstyle:executablestatementcount", "checkstyle:methodlength"})
public void someLongMethod() throws Exception {
Run Code Online (Sandbox Code Playgroud)

注意: " @SuppressWarnings"前缀是可选的(但建议).根据文档,参数名称必须全部小写,但是练习表明任何情况都有效.

  • 请记住在TreeWalter上添加FileContentsHolder.请参见http://stackoverflow.com/a/5764666/480483 (7认同)
  • 如果您使用`//CHECKSTYLE.OFF:`然后忘记再次打开它,它将仅在包含`//CHECKSTYLE.OFF:`的文件中还是在所有随后处理的文件中保持选中状态吗? (2认同)
  • “参数名称必须全部小写。” `@SuppressWarnings("checkstyle:VariableDeclarationUsageDistance")` 对我来说与小写等效项一样好。 (2认同)
  • 由于[checkstyle 8.1](http://checkstyle.sourceforge.net/releasenotes.html#Release_8.1),[SuppressionCommentFilter](http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter)应该位于` TreeWalker”和“ FileContentHolder”不再是必需的(可用)。 (2认同)

Hen*_*ger 64

如果您更喜欢使用注释来选择性地使规则静音,现在可以使用@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>
Run Code Online (Sandbox Code Playgroud)

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

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

<module name="TreeWalker">
...
Run Code Online (Sandbox Code Playgroud)

现在,您可以注释例如要从某个Checkstyle规则中排除的方法:

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

checkstyle:参数中的前缀@SuppressWarnings是可选的,但我喜欢它作为此警告来自的提醒.规则名称必须小写.

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

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

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

Preferences:
  Java
  --> Compiler
  --> Errors/Warnings
  --> Annotations
  --> Unhandled token in '@SuppressWarnings': set to 'Ignore'
Run Code Online (Sandbox Code Playgroud)

  • 我提名这个作为检查答案,因为我认为这是在大多数情况下应该最有效的解决方案. (2认同)

Ako*_* Cz 33

同样有效的是SuppressWithNearbyCommentFilter,它使用单独的注释来抑制审计事件.

例如

// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
public void onClick(View view) { ... }
Run Code Online (Sandbox Code Playgroud)

要配置过滤器,以便CHECKSTYLE IGNORE检查FOR NEXT var LINES,可以避免触发对当前行和下一个var行(总共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>
Run Code Online (Sandbox Code Playgroud)

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


Joa*_*zar 5

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

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

<module name="NoWhitespaceBefore"/>
Run Code Online (Sandbox Code Playgroud)

我不能使用:

@SuppressWarnings({"nowhitespacebefore"})
Run Code Online (Sandbox Code Playgroud)

但是,我必须使用:

@SuppressWarnings({"NoWhitespaceBefore"})
Run Code Online (Sandbox Code Playgroud)

为了使第一个语法起作用,checkstyle-config.xml 应该具有:

<module name="NoWhitespaceBefore">
  <property name="id" value="nowhitespacebefore"/>
</module>
Run Code Online (Sandbox Code Playgroud)

这对我有用,至少在 CheckStyle 6.17 版中如此。