什么是Java中的SuppressWarnings(“未选中”)?

本文翻译自:What is SuppressWarnings (“unchecked”) in Java?

Sometime when looking through code, I see many methods specify an annotation: 在查看代码的某个时候,我看到许多方法都指定了注释:

@SuppressWarnings("unchecked")

What does this mean? 这是什么意思?


#1楼

参考:https://stackoom.com/question/4juV/什么是Java中的SuppressWarnings-未选中


#2楼

It is an annotation to suppress compile warnings about unchecked generic operations (not exceptions), such as casts. 它是一个注释,用于抑制有关未经检查的泛型操作(非异常)的编译警告,例如强制转换。 It essentially implies that the programmer did not wish to be notified about these which he is already aware of when compiling a particular bit of code. 它本质上意味着程序员不希望在编译特定代码时通知他已经知道的这些内容。

You can read more on this specific annotation here: 您可以在此处阅读有关此特定注释的更多信息:

SuppressWarnings SuppressWarnings

Additionally, Oracle provides some tutorial documentation on the usage of annotations here: 此外,Oracle还提供了一些有关注释用法的教程文档:

Annotations 注释

As they put it, 正如他们所说,

"The 'unchecked' warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled Generics)." “当与泛型出现之前编写的遗留代码接口时,可能会发生'未经检查'警告(在标题为泛型的课程中讨论过)。”


#3楼

Sometimes Java generics just doesn't let you do what you want to, and you need to effectively tell the compiler that what you're doing really will be legal at execution time. 有时Java泛型只是没有让你做你想要什么,你需要有效地告诉编译器,你在做什么真的在执行时的法律。

I usually find this a pain when I'm mocking a generic interface, but there are other examples too. 当我嘲笑通用界面时,我通常会发现这很痛苦,但也有其他例子。 It's usually worth trying to work out a way of avoiding the warning rather than suppressing it (the Java Generics FAQ helps here) but sometimes even if it is possible, it bends the code out of shape so much that suppressing the warning is neater. 通常值得尝试找出一种避免警告而不是抑制警告的方法( Java Generics常见问题解答在这里有帮助),但有时即使可能,它也会使代码变形,以至于抑制警告更加整洁。 Always add an explanatory comment in that case! 在这种情况下总是添加解释性注释!

The same generics FAQ has several sections on this topic, starting with "What is an "unchecked" warning?" 相同的泛型FAQ有关于此主题的几个部分,从“什么是未经检查的”警告开始? - it's well worth a read. - 非常值得一读。


#4楼

The SuppressWarning annotation is used to suppress compiler warnings for the annotated element. SuppressWarning注释用于抑制带注释元素的编译器警告。 Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts. 具体来说, unchecked类别允许抑制由于未经检查的类型转换而生成的编译器警告。


#5楼

As far I know, for now it has to do with suppressing warnings about generics; 据我所知,目前它与抑制有关仿制药的警告有关; generics are a new programming construct not supported in JDK versions earlier than JDK 5, so any mixes of the old constructs with the new ones might pose some unexpected results. 泛型是JDK 5之前的JDK版本中不支持的新编程构造,因此旧构造与新构造的任何混合可能会产生一些意想不到的结果。

The compiler warns the programmer about it, but if the programmer already knows, they can turn those dreaded warnings off using SuppressWarnings. 编译器会向程序员发出警告,但如果程序员已经知道,他们可以使用SuppressWarnings关闭那些可怕的警告。


#6楼

It could also mean that the current Java type system version isn't good enough for your case. 这也可能意味着当前的Java类型系统版本不适合您的情况。 There were several JSR propositions / hacks to fix this: Type tokens, Super Type Tokens , Class.cast(). 有几个JSR命题 /黑客来解决这个问题:类型标记, 超类型标记 ,Class.cast()。

If you really need this supression, narrow it down as much as possible (eg don't put it onto the class itself or onto a long method). 如果你真的需要这种抑制,尽可能地缩小范围(例如,不要把它放在类本身或长方法上)。 An example: 一个例子:

public List<String> getALegacyListReversed() {
   @SuppressWarnings("unchecked") List<String> list =
       (List<String>)legacyLibrary.getStringList();

   Collections.reverse(list);
   return list;
}
J2SE 提供的最后一个批注是 @SuppressWarnings。该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。 一点背景:J2SE 5.0 为 Java 语言增加了几个新的特性,并且和它们一起增加了许多新的警告并承诺在将来增加更多的警告。您可以为 "javac" 增加 -Xlint 参数来控制是否报告这些警告(如上面的 @Deprecated 部分所示)。 默认情况下,Sun 编译器以简单的两行的形式输出警告。通过添加 -Xlint:keyword 标记(例如 -Xlint:finally),您可以获得关键字类型错误的完整说明。通过在关键字前面添加一个破折号,写为 -Xlint:-keyword,您可以取消警告。(-Xlint 支持的关键字的完整列表可以在 javac 文档页面上找到。)下面是一个清单: 关键字 用途 deprecation 使用了不赞成使用的类或方法时的警告 unchecked 执行了检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型。 fallthrough 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。 path 在类路径、源文件路径等有不存在的路径时的警告。 serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告。 finally 任何 finally 子句不能正常完成时的警告。 all 关于以上所有情况的警告。 @SuppressWarnings 批注允许您选择性地取消特定代码段(即,类或方法)的警告。其的想法是当您看到警告时,您将调查它,如果您确定它不是问题,您就可以添加一个 @SuppressWarnings 批注,以使您不会再看到警告。虽然它听起来似乎会屏蔽潜在的错误,但实际上它将提高代码安全性,因为它将防止您对警告无动于衷 — 您看到的每一个警告都将值得注意。 下面是使用 @SuppressWarnings 来取消 deprecation 警告的一个例子: public class DeprecatedExample2 { @Deprecated public static void foo() { }}public class DeprecatedUser2 { @SuppressWarnings(value={"deprecation"})public static void main(String[] args) { DeprecatedExample2.foo(); }} @SuppressWarnings 批注接收一个 "value" 变量,该变量是一个字符串数组,它指示将取消的警告。合法字符串的集合随编译器而变化,但在 JDK 上,可以传递给 -Xlint 的是相同的关键字集合(非常方便)。并且要求编译器忽略任何它们不能识别的关键字,这在您使用一些不同的编译器时非常方便。 因为 @SuppressWarnings 批注仅接收一个参数,并为该参数使用了特殊的名称 "value",所以您可以选择省略 value=,作为一种方便的缩写: public class DeprecatedUser2 { @SuppressWarnings({"deprecation"})public static void main(String[] args) { DeprecatedExample2.foo(); }} 您可以将单个数组参数的任意数量的字符串值传递给批注,并在任何级别上放置批注。例如,以下示例代码指示将取消整个类的 deprecation 警告,而仅在 main() 方法代码内取消 unchecked 和 fallthrough 警告: import java.util.*;@SuppressWarnings({"deprecation"})public class NonGenerics { @SuppressWarnings({"unchecked","fallthrough"})public static void main(String[] args) { Runtime.runFinalizersOnExit(); List list = new ArrayList(); list.add("foo"); } public static void foo() { List list = new ArrayList(); list.add("foo"); }} @SuppressWarnings 是否比前两个批注更有用?绝对是这样。不过,在 JDK 1.5.0 版本还没有完全支持该批注,如果您用 1.5.0 来尝试它,那么它将类似无操作指令。调用 -Xlint:-deprecation 也没有任何效果。Sun 没有声明什么时候将增加支持,但它暗示这将在即将推出的一个 dot 版本实现。 更进一步 如果您试图在 Javadocs 页面查看这些属性,那么您可能很难找到它们。它们位于核心的 java.lang,但有点隐蔽,它们出现在 Javadoc 类的最底端,列在 Exceptions 和 Errors 后面。 注意到了附加在 SuppressWarnings 批注后面的陌生的批注 @Target 和 @Retention 了吗?这些称为元数据批注,它们描述了该批注在哪里适用。我将在本系列的第二篇文章介绍它们,以及介绍如何将元数据批注应用到您自己的批注
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值