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

编程入门 行业动态 更新时间:2024-10-26 02:37:37
本文介绍了如何为特定代码行禁用特定的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 (参见严格控制Java代码质量)

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

推荐答案

查看在 checkstyle.sourceforge/config_filters.html#SuppressionCommentFilter。您需要将模块添加到checkstyle.xml

Check out the use of the supressionCommentFilter at checkstyle.sourceforge/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规则?

本文发布于:2023-11-23 03:32:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   规则   代码   checkstyle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!