我的项目中配置了checkstyle验证规则,禁止使用3个以上的输入参数定义类方法。该规则适用于我的类,但有时我必须扩展第三方类,这些类不遵守此特定规则。
是否有可能指示“checkstyle”某个方法应该被忽略?
顺便说一句,我最终得到了自己的checkstyle包装器:qulice.com(参见Strict Control of Java Code Quality)答案 0 :(得分:261)
在http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter查看supressionCommentFilter的使用情况。您需要将模块添加到checkstyle.xml
<module name="SuppressionCommentFilter"/>
并且它是可配置的。因此,您可以在代码中添加注释以关闭checkstyle(在各个级别),然后通过在代码中使用注释再次返回。 E.g。
//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中提供的另一种方法是通过@SuppressWarnings
java注释来抑制违规。为此,您需要在配置文件中使用新模块(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 {
NB:“checkstyle:
”前缀是可选的(但建议使用)。根据文档,参数名称必须全部小写,但是练习表明任何情况都有效。
答案 1 :(得分:58)
如果您更喜欢使用注释来有选择地使规则静音,现在可以使用@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>
接下来,在那里启用SuppressWarningsFilter
(作为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
}
checkstyle:
参数中的@SuppressWarnings
前缀是可选的,但我喜欢它作为此警告来自的提示。规则名称必须小写。
最后,如果你正在使用Eclipse,它会抱怨它不为人知的论点:
不支持的@SuppressWarnings(“checkstyle:methodlength”)
如果您愿意,可以在首选项中禁用此Eclipse警告:
Preferences:
Java
--> Compiler
--> Errors/Warnings
--> Annotations
--> Unhandled token in '@SuppressWarnings': set to 'Ignore'
答案 2 :(得分:32)
SuppressWithNearbyCommentFilter使用单独的注释来抑制审核事件也很有效。
例如
// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
public void onClick(View view) { ... }
配置过滤器以便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>
答案 3 :(得分:2)
每个引用SuppressWarningsFilter的答案都缺少重要细节。如果在checkstyle-config.xml中定义了全小写ID,则只能使用全小写ID。如果不是,则必须使用原始模块名称。
例如,如果在我的checkstyle-config.xml中,我有:
<module name="NoWhitespaceBefore"/>
我无法使用:
@SuppressWarnings({"nowhitespacebefore"})
但是,我必须使用:
@SuppressWarnings({"NoWhitespaceBefore"})
为了使第一种语法起作用,checkstyle-config.xml应具有:
<module name="NoWhitespaceBefore">
<property name="id" value="nowhitespacebefore"/>
</module>
这对我有用,至少在CheckStyle版本6.17中。
答案 4 :(得分:1)
我上面的答案有困难,可能是因为我将checkStyle警告设置为错误。做了什么工作是SuppressionFilter:http://checkstyle.sourceforge.net/config_filters.html#SuppressionFilter
缺点是行范围存储在单独的suppresssions.xml文件中,因此不熟悉的开发人员可能不会立即建立连接。
答案 5 :(得分:1)
<module name="Checker">
<module name="SuppressionCommentFilter"/>
<module name="TreeWalker">
<module name="FileContentsHolder"/>
</module>
</module>
配置过滤器以禁止包含行BEGIN GENERATED CODE的注释与包含END GENERATED CODE行的注释之间的审核事件:
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="BEGIN GENERATED CODE"/>
<property name="onCommentFormat" value="END GENERATED CODE"/>
</module>
//BEGIN GENERATED CODE
@Override
public boolean equals(Object obj) { ... } // No violation events will be reported
@Override
public int hashCode() { ... } // No violation events will be reported
//END GENERATED CODE
答案 6 :(得分:0)
您可以尝试 https://checkstyle.sourceforge.io/config_filters.html#SuppressionXpathFilter
使用带有-g选项的CLI生成Xpath抑制。然后,选择特定于您要抑制的行的抑制元素。将其保存在禁止文件中,并在上方的SuppressionXpathFilter元素中指定该文件路径。
https://checkstyle.sourceforge.io/cmdline.html#Command_line_usage
如果您使用的是ant,则可以将此帖子称为ant任务,用于生成禁止Xpath文件。
https://github.com/checkstyle/checkstyle/issues/6934#issuecomment-522289083
查看此线程以了解如何使用过滤器:
https://groups.google.com/forum/m/#!topic/checkstyle/F_f6R_Qk1EM
答案 7 :(得分:0)
如果您使用来自 qulice
mvn 插件 (https://github.com/teamed/qulice) 的 checkstyle,您可以使用以下抑制:
// @checkstyle <Rulename> (N lines)
... code with violation(s)
或
/**
* ...
* @checkstyle <Rulename> (N lines)
* ...
*/
... code with violation(s)