今天看到小哥哥在我的代码里加了@SuppressWarnings("rawtypes")遂有些好奇(这个鬼东西是干吗用的啊),于是在网上问了一下度娘,娘说:
1.SuppressWarnings来源?
java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。
2.SuppressWarnings用来干什么?
用于抑制编译器产生警告信息。
3.SuppressWarnings用在哪儿?
@SuppressWarnings("***")可标注在类、字段、方法、参数、构造方法,以及局部变量上。
4.SuppressWarnings可以抑制哪些警告?
关键字 | 用途 |
all | to suppress all warnings |
boxing | to suppress warnings relative to boxing/unboxing operations |
cast | to suppress warnings relative to cast operations |
dep-ann | to suppress warnings relative to deprecated annotation |
deprecation | to suppress warnings relative to deprecation |
fallthrough | to suppress warnings relative to missing breaks in switch statements |
finally | to suppress warnings relative to finally block that don’t return |
hiding | to suppress warnings relative to locals that hide variable |
incomplete-switch | to suppress warnings relative to missing entries in a switch statement (enum case) |
nls | to suppress warnings relative to non-nls string literals |
null | to suppress warnings relative to null analysis |
rawtypes | to suppress warnings relative to un-specific types when using generics on class params |
restriction | to suppress warnings relative to usage of discouraged or forbidden references |
serial | to suppress warnings relative to missing serialVersionUID field for a serializable class |
static-access | o suppress warnings relative to incorrect static access |
synthetic-access | to suppress warnings relative to unoptimized access from inner classes |
unchecked | to suppress warnings relative to unchecked operations |
unqualified-field-access | to suppress warnings relative to field access unqualified |
unused | to suppress warnings relative to unused code |
5.SuppressWarnings有哪些写法?
SuppressWarnings("***")抑制单一类型警告
SuppressWarnings(value={"***","***"})抑制多类型警告
6.SuppressWarnings示例
@SuppressWarnings("rawtypes") ,rawtypes是说传参时也要传递带泛型的参数。
@SuppressWarnings("unchecked") ,告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。
@SuppressWarnings("unused") ,告诉编译器忽略未使用变量的警告信息。