重庆的电子商务网站,阿里指数app下载,2024免费推广网站,cco网站素材Java基础 - 常用注解SuppressWarings 文章目录 Java基础 - 常用注解SuppressWarings一、概述二、 SuppressWarings注解使用2.1. 抑制单类型的警告2.2. 抑制多类型的警告2.3. 抑制所有类型的警告 四、注解目标五、抑制警告的关键字 一、概述
java.lang.SuppressWarnings是J2SE5…Java基础 - 常用注解SuppressWarings 文章目录 Java基础 - 常用注解SuppressWarings一、概述二、 SuppressWarings注解使用2.1. 抑制单类型的警告2.2. 抑制多类型的警告2.3. 抑制所有类型的警告 四、注解目标五、抑制警告的关键字 一、概述
java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类字段、方法、构造方法以及局部变量上。
SuppressWarings作用告诉编译器忽略指定的警告不用在编译完成后出现警告信息。
SuppressWarings使用
SuppressWarnings(“”)SuppressWarnings({})SuppressWarnings(value{})
value -将由编译器在注释的元素中取消显示的警告集。允许使用重复的名称。忽略第二个和后面出现的名称。出现未被识别的警告名 不是错误编译器必须忽略无法识别的所有警告名。但如果某个注释包含未被识别的警告名那么编译器可以随意发出一个警告。
各编译器供应商应该将它们所支持的警告名连同注释类型一起记录。鼓励各供应商之间相互合作确保在多个编译器中使用相同的名称。
常用注解演示 SuppressWarnings(“unchecked”) 告诉编译器忽略unchecked警告信息如使用List、ArrayList等未进行参数化产生的警告信息。 SuppressWarnings(“serial”) 告诉编译器忽略The serializable class WmailCalendar does notdeclare a static final serialVersionUID field of type long信息。 SuppressWarnings(“deprecation”) 告诉编译器忽略使用注解Deprecated警告信息 SuppressWarnings(“unchecked”, “deprecation”) 告诉编译器同时忽略unchecked和deprecation的警告信息。
二、 SuppressWarings注解使用
2.1. 抑制单类型的警告
SuppressWarnings(unchecked)
public void annotationDemo(String item){SuppressWarnings(rawtypes)List items new ArrayList();items.add(item);
}2.2. 抑制多类型的警告
SuppressWarnings(value{unchecked, rawtypes})
public void annotationDemo(String item){List items new ArrayList();items.add(item);
}2.3. 抑制所有类型的警告
SuppressWarnings(value{all})
public void annotationDemo(String item){List items new ArrayList();items.add(item);
}四、注解目标
通过 SuppressWarnings 的源码可知其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。
建议注解应声明在最接近警告发生的位置。
五、抑制警告的关键字
序列关键字说明1allto suppress all warnings 抑制所有警告2boxingto suppress warnings relative to boxing/unboxing operations抑制装箱、拆箱操作时候的警告3castto suppress warnings relative to cast operations 抑制映射相关的警告4dep-annto suppress warnings relative to deprecated annotation抑制启用注释的警告5deprecationto suppress warnings relative to deprecation抑制过期方法警告6fallthroughto suppress warnings relative to missing breaks in switch statements抑制确在switch中缺失breaks的警告7finallyto suppress warnings relative to finally block that don’t return 抑制finally模块没有返回的警告8hidingto suppress warnings relative to locals that hide variable9incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句)10nlsto suppress warnings relative to non-nls string literals(忽略非nls格式的字符)11nullto suppress warnings relative to null analysis(忽略对null的操作)12rawtypesto suppress warnings relative to un-specific types when using generics on class params(使用generics时忽略没有指定相应的类型)13restrictionto suppress warnings relative to usage of discouraged or forbidden references14serialto suppress warnings relative to missing serialVersionUID field for a serializable class(忽略在serializable类中没有声明serialVersionUID变量15static-accessto suppress warnings relative to incorrect static access抑制不正确的静态访问方式警告)16synthetic-accessto suppress warnings relative to unoptimized access from inner classes抑制子类没有按最优方法访问内部类的警告17uncheckedto suppress warnings relative to unchecked operations抑制没有进行类型检查操作的警告18unqualified-field-accessto suppress warnings relative to field access unqualified 抑制没有权限访问的域的警告19unusedto suppress warnings relative to unused code 抑制没被使用过的代码的警告