如何为特定代码行禁用特定的checkstyle规则? [英] How to disable a particular checkstyle rule for a particular line of code?

查看:270
本文介绍了如何为特定代码行禁用特定的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>

,允许您关闭模式匹配资源的单独检查。

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注释。为此,您需要在配置文件中使用新模块(SuppressWarningsFilter和SuppressWarningsHolder):

Another method, now available in Checkstyle 5.7 is to suppress violations via the @SuppressWarnings 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> 

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

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 {

NB: 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天全站免登陆