4

我知道我可以抑制来自 IntelliJ IDEA 检查的警告,如下所示:

@SuppressWarnings("CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

对于来自外部的人来说,可能不清楚需要哪种工具进行这种抑制。

PMD 为此使用前缀:

@SuppressWarnings("PMD.UnusedLocalVariable")

FindBugs 使用专用注解:

@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")

有什么方法可以清楚地表明这种抑制仅适用于 IntelliJ IDEA?

4

2 回答 2

3

一种抑制方法参数的方法是使用 javadoc 标记:

/**
 * @noinspection CollectionDeclaredAsConcreteClass
 */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

这个 javadoc 标签是非常特定于 IntelliJ 的,所以其他工具都不应该有任何问题。您可以通过直接识别它或添加一些注释来轻松识别它:

/**
 * @noinspection IntelliJ CollectionDeclaredAsConcreteClass
 */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

当然,您可以进一步缩短它:

/** @noinspection CollectionDeclaredAsConcreteClass */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

一般的做法

可以使用特殊注释而不是注释逐个抑制某些警告@SuppressWarnings

这是一个带有很多警告的示例:

在此处输入图像描述

如果按Alt+ EnternotUsedLocalVariable则可以Suppress for statement with comment在上下文菜单上进行选择(在选择 时向右后Remove variable ...)。

在某些变量/字段/方法上,选定的抑制只是Suppress for statement.

现在,如果您查看右侧字段,则大多数(不是全部)警告已被抑制:

在此处输入图像描述

如您所见,同一注释行上可能有多个抑制。

这似乎有点乏味,但我认为这是你能做的最好的。

于 2015-09-03T08:22:10.437 回答
3

我找到了一种方法来实现这一点@SuppressWarnings

@SuppressWarnings("idea: CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

注意后面一定要有空格idea:,否则不起作用。

于 2015-09-03T19:26:05.253 回答