2

What is wrong with the SuppressWarnings annotation above the if statement? Eclipse with Sun JDK 6 provides two syntax error descriptions, both unhelpful and hard to understand, shown in comments.

class TestDeadCode
{
    //@SuppressWarnings("all")
    public static void main(String[] args)
    {
        @SuppressWarnings("all")  // syntax errors: insert enum body, insert enum id
        if ((Constants.flag0) && (Constants.flag1))
            System.out.println("hello\n");      
    }
}

interface Constants
{
    boolean flag0 = false;
    boolean flag1 = false;
}
4

1 回答 1

8

只有类、方法、变量声明、参数和包可以被注释。因此,您不能在 if 语句中使用 SuppressWarnings("all")。

要解决此问题,您只需执行以下操作。

@SuppressWarnings("all")
boolean flag = Constants.flag0 && Constants.flag1;
if (flag) {
    System.out.println("hello\n");
}

目前还没有 SuppressWarnings("Dead code") 。

http://docs.oracle.com/javase/7/docs/api/java/lang/SuppressWarnings.html http://pmd.sourceforge.net/suppressing.html

于 2012-01-14T07:13:42.267 回答