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

发布于 2024-09-18 07:59:51 字数 488 浏览 6 评论 0 原文

我搬到了一台装有最新 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")。另外,在某些地方,默认情况下,最新的编译器让我同时放置“未检查”和“原始类型” - 使用旧编译器编译该代码会产生相同的警告。

如何强制两者之间的向后/向前兼容性,以便编译器都不产生警告?

I moved to a new machine which has the latest Sun's Java compiler and noticed some warnings in the existing Java 6 code. The Eclipse IDE, suggested that I annotate the assignment with:

@SuppressWarnings("rawtypes")

For example:

class Foo<T> {
...
}
...
@SuppressWarnings("rawtypes")
Foo foo = new Foo();

When I moved back to the machine with the older compiler (JDK 1.6.0_20), I have noticed that this older compiler now warns about the suppression of "rawtypes" warnings, claiming that this suppression is unsupported and proposing to replace it with @SuppressWarnings("unchecked"). Also, there were some places which the newest compiler, by default, made me to put both "unchecked" and "rawtypes" - compiling that code with the older compiler reproduces the same warning.

How can I enforce backward/forward compatibility between the two, so that neither compiler produces warnings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2024-09-25 07:59:51

您可以使用 eclipse 编译器和 javac 都支持的 @SuppressWarnings("unchecked")

但请记住,@SuppressWarnings 注释是由编译器使用的,它可以有自己的值。 JLS 仅强制编译器理解“未选中”和“已弃用”值(目前)。

编译器供应商应记录他们支持的与此注释类型相关的警告名称。 鼓励他们合作,以确保相同的名称可以在多个编译器中工作

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

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


资源:


编辑:这是现在不可用的 knol用作参考的文章,最初由 Alex Miller 撰写。

Java 中的 @SuppressWarnings 注解

用于抑制各种警告的标准注释

SuppressWarnings 注释已作为标准注释添加到 Java SE 5 中。

定义

@SuppressWarnings 注释在 Java 语言规范 第 9.6.1.5 节。本节规定:

<块引用>

注释类型SuppressWarnings支持程序员控制由Java编译器发出的警告。它包含一个 String 数组元素。如果程序声明使用注释 @SuppressWarnings(value = {S1, ... , Sk}) 进行注释,则 Java 编译器不得报告由 S1、... 之一标识的任何警告。 , Sk 是否会因带注释的声明或其任何部分而生成该警告。

未检查的警告由字符串“unchecked”标识。

后续部分 @Deprecation 还提到可以使用 @SuppressWarnings("deprecation") 抑制这些警告。

有效的警告类型

规范本身中唯一提到的两个警告字符串是“unchecked”和“deprecation”。然而,Sun JDK 在编译器中使用更大的字符串集。您可以通过执行以下命令来确定当前集:

<前><代码>javac -X

它将向您显示(除其他外)-Xlint 的有效设置。

例如,Sun JDK 1.5 显示:

  • all - 禁止此代码中的所有警告
  • 弃用 - 禁止使用已弃用的代码发出警告
  • 未选中 - 禁止来自未选中的调用或未选中的强制转换的警告
  • fallthrough - 如果开关在没有找到有效案例(并且没有默认值)的情况下失败,则抑制警告
  • 路径 -
  • serial - 如果可序列化类未定义serialVersionUID,则抑制警告
  • finally - 禁止在finally中返回警告(这将忽略try中的返回)

Sun JDK 1.6 添加了:

  • 演员
  • divzero - 如果检测到整数除以零,则抑制警告
  • 覆盖

IDE 和静态分析工具通常支持 @SuppressWarnings 的大量其他可能值。这些值对应于 IDE 执行的特定静态分析检查。

日食

Eclipse 3.3 的 Eclipse 警告值为 记录在 JDT 文档中。

  • all - 抑制所有警告
  • 装箱 - 抑制与装箱/拆箱操作相关的警告
  • cast - 抑制与强制转换操作相关的警告
  • dep-ann - 抑制与已弃用注释相关的警告
  • 弃用 - 抑制与弃用相关的警告
  • fallthrough - 抑制与 switch 语句中缺少中断相关的警告
  • finally - 抑制与不返回的finally 块相关的警告
  • 隐藏 - 抑制与隐藏变量的局部变量相关的警告
  • incomplete-switch - 抑制与 switch 语句中缺少条目相关的警告(枚举情况)
  • nls - 抑制与非 nls 字符串文字相关的警告
  • null - 抑制与 null 分析相关的警告
  • 限制 - 抑制与使用不鼓励或禁止的引用相关的警告
  • serial - 抑制与可序列化类缺少serialVersionUID字段相关的警告
  • static-access - 抑制与不正确的静态访问相关的警告
  • synthetic-access - 抑制与来自内部类的未优化访问相关的警告
  • 未检查 - 抑制与未检查操作相关的警告
  • unqualified-field-access - 抑制与不合格字段访问相关的警告
  • 未使用 - 抑制与未使用代码相关的警告

IntelliJ

NetBeans

示例

指定单个警告的示例:

@SuppressWarnings(“未选中”)
公共无效方法WithScaryWarnings(){
    列表 rawList = new ArrayList();
    列表<字符串> stringList = (List)rawList;
}

使用两个警告的示例:

@SuppressWarnings({"unchecked","deprecation"})
公共无效方法WithScaryWarnings(){
    调用DeprecatedMethod();
}

You can use the @SuppressWarnings("unchecked") which is supported by both the eclipse compiler and javac.

But remember the @SuppressWarnings annotation is used by your compiler which can have its own values. The JLS only forces the compiler to understand the values "unchecked" and "deprecated" (for now).

Compiler vendors should document the warning names they support in conjunction with this annotation type. They are encouraged to cooperate to ensure that the same names work across multiple compilers.

If you use Helios, you will need to set a specific option to allow @SuppressWarnings("unchecked") instead of @SuppressWarnings("rawtypes"),

In case it is not possible to update the code with the new token, the suppressRawWhenUnchecked=true system property can be set when starting Eclipse.


Resources :


EDIT: Here is the now unavailable knol article that was used as a reference, originally written by 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();
}
半仙 2024-09-25 07:59:51

请注意,Eclipse 3.5 不理解原始类型并标记警告以切换到未选中状态。令人沮丧的是 Eclipse 提出了 rawtypes 注释,它导致的问题多于解决的问题。他们应该坚持使用标准的。

Note that Eclipse 3.5 doesnt understand rawtypes and flags a warning to switch to unchecked. It is frustrating that Eclipse came up with rawtypes annotation which causes more problems than solving. They should have just stuck with the standard one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文