不必要的@SuppressWarnings("未使用")

Edd*_*Edd 54 java eclipse annotations compiler-warnings suppress-warnings

@SuppressWarnings在eclipse中为代码获取了注释的编译器警告:

@Override
public boolean doSomething(@SuppressWarnings("unused") String whatever) throws AnException {
    throw new AnException("I'm still in bed and can't do anything until I've had a shower!");
}
Run Code Online (Sandbox Code Playgroud)

在"未使用"一词下看起来像一个黄色的波浪线,在鼠标悬停时,我得到了工具提示Unnecessary @SuppressWarnings("unused").

我认为另一个开发人员被提示通过eclipse输入这些注释,我基本上被提示将它们删除.我如何配置eclipse以提示我将@SuppressWarnings注释放入而不是抱怨它?

如果有人想在这里评论最佳实践,那么这也是最受欢迎的.

Ósc*_*pez 56

在您的问题中的代码中,@SuppressWarnings("unused")注释是不必要的,因为该方法要么覆盖超类中的另一个方法,要么实现接口.即使你实际上没有使用whatever参数,也必须声明它,否则@Override注释会产生错误(如果你删除了参数,你将改变被覆盖方法的签名.)

在某些旧版本的Eclipse中,所显示的代码不会引起警告,但在最近的版本中它会发出警告.我相信这是一个有效的警告,我宁愿@SuppressWarnings("unused")在这种情况下删除它.

  • 此外,在更新的版本中,"未使用"警告已经变得更加精细 - 如果存在附加到该方法的运行时保留策略的注释,则未使用的警告将被抑制(应该如此). (3认同)

aio*_*obe 23

窗口首选项Java编译器错误/警告注释.

并选择Ignore for Unused'@ SuppressWarnings`标记.


Dav*_*oni 5

或者,如果您认为删除SuppressWarnings注释更正确:

窗口 - >首选项 - > Java - >编译器 - >错误/警告 - >不必要的代码 - >参数值未使用

在覆盖和实现方法中选择忽略


Tor*_*ger 5

在我的代码中,没有使用@SuppressWarnings("unused")定义3个方法的继承

此代码在Eclipse Juno(最新版本)中提供了"不必要的@SuppressWarnings("unused")",但是如果我删除@SuppressWarnings("unused"),我会在IntelliJ IDEA 11.1中收到"Constructor/Method is never used"警告. 3

这些方法不是直接在项目中使用,只有第三方产品Jackson,JAXB和GSON,所以IntelliJ是对的,我会说......

public class EmailUnsendable extends SkjemaError {

    private NestedCommand command;        // Can't be Command (interface) because of GSON!

    @SuppressWarnings("unused")            // Used by Jackson/JAXB/GSON
    public EmailUnsendable() {
    }

    public EmailUnsendable(String referenceNumber, String stackTrace, NestedCommand command) {
        super(referenceNumber, stackTrace);
        this.command = command;
    }

    @SuppressWarnings("unused")            // Used by Jackson/JAXB/GSON
    public NestedCommand getCommand() {
        return command;
    }

    @SuppressWarnings("unused")            // Used by Jackson/JAXB/GSON
    public void setCommand(NestedCommand command) {
        this.command = command;
    }
}
Run Code Online (Sandbox Code Playgroud)

我相信这是Eclipse中的一个错误.

  • Doh,我试图在eclipse.org上提交一个bug,但网站太复杂了 - 希望有人给他们这个bug报告 (3认同)
  • 我发现这很有趣.你有没有切换到IntelliJ? (2认同)
  • @SkyKelsey我于2003年在Århus的WMdata切换到IntelliJ,并试图永远不要使用任何其他东西......即使客户要求我;-) (2认同)