Java 如何抑制 CheckStyle 中的警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3565840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:54:20  来源:igfitidea点击:

How do I Suppress Warnings in CheckStyle?

javaeclipsecheckstyle

提问by Tom Tresansky

I'm using the CheckStyle plugin for Eclipse.

我正在使用 Eclipse 的 CheckStyle 插件。

It's great at finding things I didn't intend 99% of the time, but the 1% of the time I actually did intend to knowingly violate a rule, I would like to let CheckStyle know it need not concern itself with flagging a warning.

它非常适合在 99% 的情况下找到我不打算做的事情,但在 1% 的情况下我确实打算故意违反规则,我想让 CheckStyle 知道它不需要关心自己标记警告。

Example: the Missing a Javadoc comment rule. Most of the time I want Javadoc comments on my methods. However, a method such as:

示例:缺少 Javadoc 注释规则。大多数时候,我希望对我的方法进行 Javadoc 注释。但是,一种方法,例如:

public boolean isValid() {
  return valid;
}

can probably get by without one.

没有一个可能会过得去。

Is there anything like the @SuppressWarningsannotation which can be used to flag a specific CheckStyle rule violation as acceptable? Some sort of specially formatted comment, maybe? I don't want to disable the rule, I just want to ignore a specific violation.

是否有类似@SuppressWarnings注释可用于将特定的 CheckStyle 规则违规标记为可接受的内容?也许是某种特殊格式的评论?我不想禁用规则,我只想忽略特定的违规行为。

(I realize in this case I could just write the Javadoc comment, but in other cases fixing the rule violation isn't so simple).

(我意识到在这种情况下我可以只写 Javadoc 注释,但在其他情况下修复规则违规并不是那么简单)。

采纳答案by Synthesis

Seems pretty tedious but there needs to be explicit XML configuration to ignore it. You can probably find a GUI to do it via using the Checkstyle plugin for Eclipse or Netbeans. The example I've found is on the Checkstyle configurationpage.

看起来很乏味,但需要有显式的 XML 配置才能忽略它。您可能可以通过使用 Eclipse 或 Netbeans 的 Checkstyle 插件找到一个 GUI 来执行此操作。我发现的示例在 Checkstyle配置页面上。

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
    <suppress checks="JavadocStyleCheck"
              files="AbstractComplexityCheck.java"
              lines="82,108-122"/>
    <suppress checks="MagicNumberCheck"
              files="JavadocStyleCheck.java"
              lines="221"/>
</suppressions>

回答by PhiLho

Synthesis pointed to the Checkstyle configuration page. Skimming it, I found SuppressWithNearbyCommentFilterwhich seems promising, unless I misunderstood its purpose...

Synthesis 指向 Checkstyle 配置页面。略读一下,我发现SuppressWithNearbyCommentFilter这似乎很有希望,除非我误解了它的目的......

回答by Aaron

PhiLho is right - SuppressWithNearbyCommentFilter or SuppressionCommentFilter can help. I have SuppressionCommentFilter configured and adding the comments "CHECKSTYLE:OFF" and "CHECKSTYLE:ON" will disable check style temporarily.

PhiLho 是对的 - SuppressWithNearbyCommentFilter 或 SuppressionCommentFilter 可以提供帮助。我配置了 SuppressionCommentFilter 并添加注释“CHECKSTYLE:OFF”和“CHECKSTYLE:ON”将暂时禁用检查样式。