标签:选项 Java SuppressWarnings warnings suppress relative 注解 警告
概述
作用:用于抑制编译器产生警告信息,即IDEA里面的黄色代码块。
抑制单类型的警告:
@SuppressWarnings("unchecked")
public void add(String item) {
List items = new ArrayList();
items.add(item);
}
抑制多类型的警告:
@SuppressWarnings(value={"unchecked", "rawtypes"})
public void add(String item) {
List items = new ArrayList();
items.add(item);
}
抑制所有类型的警告:
@SuppressWarnings("all")
通过 @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 | to 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 |
Java Lint选项
- lint的含义
用于在编译程序的过程中,进行更细节的额外检查。 - javac 的标准选项和非标准选项
标准选项:是指当前版本和未来版本中都支持的选项,如 -cp 和 -d 等。
非标准选项:是指当前版本支持,但未来不一定支持的选项。通过 javac -X 查看当前版本支持的非标准选项。
- 查看警告信息
默认情况下执行 javac 仅仅显示警告的扼要信息,也不过阻止编译过程。若想查看警告的详细信息,则需要执行javac -Xlint:keyword
来编译源码。
总结
虽然,Java语言提供此注解,但是强烈建议永远不要使用:
- 对于泛型,一定要指定泛型类型。而不是使用raw type,即Object类型,
List a = new ArrayList();
,再加上SuppressWarnings注解。因为假如后面如果想要从List里面取元素出来,可能出现错误的将String元素转型为Integer,造成classCastException,或者NumberFormatException; - 对于无用的方法或者类,立马删除,而不是加
@SuppressWarnings("unused")
,脏代码会破坏阅读体验;
标签:选项,Java,SuppressWarnings,warnings,suppress,relative,注解,警告 来源: https://blog.csdn.net/lonelymanontheway/article/details/120129912
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。