IntelliJ IDEA @SuppressWarnings用于检查工具名称

时间:2015-09-02 21:17:25

标签: java intellij-idea suppress-warnings code-inspection

我知道我可以抑制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?

2 个答案:

答案 0 :(得分: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注释逐个禁止某些警告。

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

enter image description here

如果您在notUsedLocalVariable Alt + Enter ,则可以在上下文菜单中选择Suppress for statement with comment(选择{{ 1}})。

在某些变量/字段/方法上,所选抑制仅为Remove variable ...

现在,如果你查看右侧字段,大多数(并非所有)警告都被抑制了:

enter image description here

正如您所看到的,在同一评论行上可以有多个抑制。

这看起来有点单调乏味,但我认为这是你能做的最好的事情。

答案 1 :(得分:3)

我找到了一种如何使用@SuppressWarnings

实现这一目标的方法
@SuppressWarnings("idea: CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

请注意idea:后必须有空格,否则无效。

相关问题