Java 6:不支持的@SuppressWarnings("rawtypes")警告

47
我换了一台新机器,里面有最新的Sun Java编译器,并且注意到现有的Java 6代码中有一些警告。Eclipse IDE建议我使用注释来标记赋值语句:
@SuppressWarnings("rawtypes")
例如:
class Foo<T> {
...
}
...
@SuppressWarnings("rawtypes")
Foo foo = new Foo();

我刚刚回到老版本的编译器(JDK 1.6.0_20)所在的机器上时,发现这个旧版本的编译器现在警告"rawtypes"警告被禁止,并提示用 @SuppressWarnings("unchecked") 代替。此外,有些地方最新的编译器默认要求同时加上 "unchecked" 和 "rawtypes",使用旧版本的编译器编译该代码会产生同样的警告。

我如何确保两个版本之间的向前/向后兼容性,以便两个编译器都不会产生警告?


1
哪个版本的Eclipse建议使用rawtypes?我从未见过这样的建议。据我记得,每当我遇到这种情况时,Eclipse总是建议使用unchecked - musiKk
2个回答

50

您可以使用@SuppressWarnings("unchecked"),它被Eclipse编译器和javac编译器都支持。

但请记住,@SuppressWarnings注释由编译器使用,编译器可能有自己的值。JLS只强制编译器理解"unchecked"和"deprecated"(目前为止)这些值。

编译器供应商应该记录他们在与此注释类型一起支持的警告名称。他们被鼓励合作,以确保相同的名称适用于多个编译器

如果您使用的是Helios,则需要设置特定选项才能允许@SuppressWarnings("unchecked")而不是@SuppressWarnings("rawtypes")

如果无法使用新令牌更新代码,则可以在启动Eclipse时设置suppressRawWhenUnchecked=true系统属性。


资源:


编辑:这是一篇现在已经无法获得的文章,最初由Alex Miller撰写,被用作参考。

@SuppressWarnings Annotation in Java

Standard annotation for suppressing various warnings

The SuppressWarnings annotation was added as a standard annotation in Java SE 5.

Definition

The @SuppressWarnings annotation is defined in the Java Language Specification section 9.6.1.5. This section states:

The annotation type SuppressWarnings supports programmer control over warnings otherwise issued by the Java compiler. It contains a single element that is an array of String. If a program declaration is annotated with the annotation @SuppressWarnings(value = {S1, ... , Sk}), then a Java compiler must not report any warning identified by one of S1, ... , Sk if that warning would have been generated as a result of the annotated declaration or any of its parts.

Unchecked warnings are identified by the string "unchecked".

The subsequent section on @Deprecation also mentions that these warnings can be suppressed with @SuppressWarnings("deprecation").

Valid Warning Types

The only two warning strings that are mentioned in the specification itself are "unchecked" and "deprecation". However, the Sun JDK uses a larger set of strings in the compiler. You can determine the current set by executing:

javac -X

which will show you (among other things) the valid settings for -Xlint.

For example, Sun JDK 1.5 shows:

  • all - suppress all warnings from this code
  • deprecation - suppress warnings from using deprecated code
  • unchecked - suppress warnings from an unchecked call or an unchecked cast
  • fallthrough - suppress warnings if a switch falls through without finding a valid case (and no default)
  • path -
  • serial - suppress warnings if a Serializable class does not define a serialVersionUID
  • finally - suppress warnings from return within a finally (which will ignore return with the try)

And Sun JDK 1.6 adds:

  • cast
  • divzero - suppress warnings if integer divide by zero is detected
  • empty
  • overrides
  • none

IDEs and static analysis tools typically support a large number of other possible values for @SuppressWarnings. These values correspond to specific static analysis checks performed by the IDE.

Eclipse

The Eclipse warning values for Eclipse 3.3 are documented in the JDT docs.

  • all - suppress all warnings
  • boxing - suppress warnings relative to boxing/unboxing operations
  • cast - suppress warnings relative to cast operations
  • dep-ann - suppress warnings relative to deprecated annotation
  • deprecation - suppress warnings relative to deprecation
  • fallthrough - suppress warnings relative to missing breaks in switch statements
  • finally - suppress warnings relative to finally block that don't return
  • hiding - suppress warnings relative to locals that hide variable
  • incomplete-switch - suppress warnings relative to missing entries in a switch statement (enum case)
  • nls - suppress warnings relative to non-nls string literals
  • null - suppress warnings relative to null analysis
  • restriction - suppress warnings relative to usage of discouraged or forbidden references
  • serial - suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-access - suppress warnings relative to incorrect static access
  • synthetic-access - suppress warnings relative to unoptimized access from inner classes
  • unchecked - suppress warnings relative to unchecked operations
  • unqualified-field-access - suppress warnings relative to field access unqualified
  • unused - suppress warnings relative to unused code

IntelliJ

NetBeans

Examples

An example of specifying a single warning:

@SuppressWarnings("unchecked")
public void methodWithScaryWarnings() {
    List rawList = new ArrayList();
    List<String> stringList = (List<String>)rawList;
}

An example of using two warnings:

@SuppressWarnings({"unchecked","deprecation"})
public void methodWithScaryWarnings() {
    callDeprecatedMethod();
}

1
Helios中的Eclipse编译器拒绝@SuppressWarnings("unchecked")并建议Leden所说的内容。 - whiskeysierra
@Willi,你说得对,我更新了我的帖子,提供更多关于Helios的信息。 - Colin Hebert

4
请注意,Eclipse 3.5不理解原始类型并标记警告以切换到未经检查的类型。令人沮丧的是,Eclipse提出了原始类型注释,这比解决问题更加困难。他们应该坚持使用标准注释。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接