禁用特定代码行的特定 Checkstyle 规则? [英] Disable a particular Checkstyle rule for a particular line of code?

查看:44
本文介绍了禁用特定代码行的特定 Checkstyle 规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Checkstyle validation rule configured in my project, that prohibits to define class methods with more than 3 input parameters. The rule works fine for my classes, but sometimes I have to extend third-party classes, which do not obey this particular rule.

是否有可能指示 Checkstyle 静默忽略某个方法?

Is there a possibility to instruct Checkstyle that a certain method should be silently ignored?

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

BTW, I ended up with my own wrapper of Checkstyle: qulice.com (see Strict Control of Java Code Quality)

推荐答案

http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter.您需要将该模块添加到您的 checkstyle.xml

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"/>

而且它是可配置的.因此,您可以向代码添加注释以关闭 checkstyle(在各个级别),然后通过在代码中使用注释重新打开.例如

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

或者甚至更好,使用这个经过调整的版本:

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>

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

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

*注意:您还必须添加FileContentsHolder:

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

<module name="FileContentsHolder"/>

另见

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

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

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

因此,如果您的 checkstyle.xml 中有:

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>

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

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

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

现在在 Checkstyle 5.7 中可用的另一种方法是通过 @SuppressWarnings java 注释抑制违规.为此,您需要在配置文件中添加两个新模块(SuppressWarningsFilterSuppressWarningsHolder):

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

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

然后,在您的代码中,您可以执行以下操作:

Then, within your code you can do the following:

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

或者,对于多次抑制:

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

注意:checkstyle:"前缀是可选的(但推荐).根据文档,参数名称必须全部小写,但实践表明任何情况都有效.

NB: The "checkstyle:" prefix is optional (but recommended). According to the docs the parameter name have to be in all lowercase, but practice indicates any case works.

这篇关于禁用特定代码行的特定 Checkstyle 规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆