本文翻译自: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: 您可以在此处阅读有关此特定注释的更多信息:
Additionally, Oracle provides some tutorial documentation on the usage of annotations here: 此外,Oracle还提供了一些有关注释用法的教程文档:
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;
}