ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java注解@SuppressWarnings

2021-09-19 21:01:59  阅读:242  来源: 互联网

标签:选项 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 的源码可知,其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。建议注解应声明在最接近警告发生的位置。

抑制警告的关键字

关键字用途
allto suppress all warnings
boxingto suppress warnings relative to boxing/unboxing operations
castto suppress warnings relative to cast operations
dep-annto suppress warnings relative to deprecated annotation
deprecationto suppress warnings relative to deprecation
fallthroughto suppress warnings relative to missing breaks in switch statements
finallyto suppress warnings relative to finally block that don’t return
hidingto suppress warnings relative to locals that hide variable
incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)
nlsto suppress warnings relative to non-nls string literals
nullto suppress warnings relative to null analysis
rawtypesto suppress warnings relative to un-specific types when using generics on class params
restrictionto suppress warnings relative to usage of discouraged or forbidden references
serialto suppress warnings relative to missing serialVersionUID field for a serializable class
static-accessto suppress warnings relative to incorrect static access
synthetic-accessto suppress warnings relative to unoptimized access from inner classes
uncheckedto suppress warnings relative to unchecked operations
unqualified-field-accessto suppress warnings relative to field access unqualified
unusedto suppress warnings relative to unused code

Java Lint选项

  1. lint的含义
    用于在编译程序的过程中,进行更细节的额外检查。
  2. javac 的标准选项和非标准选项
    标准选项:是指当前版本和未来版本中都支持的选项,如 -cp 和 -d 等。
    非标准选项:是指当前版本支持,但未来不一定支持的选项。通过 javac -X 查看当前版本支持的非标准选项。
    在这里插入图片描述
  3. 查看警告信息
    默认情况下执行 javac 仅仅显示警告的扼要信息,也不过阻止编译过程。若想查看警告的详细信息,则需要执行javac -Xlint:keyword来编译源码。

总结

虽然,Java语言提供此注解,但是强烈建议永远不要使用:

  1. 对于泛型,一定要指定泛型类型。而不是使用raw type,即Object类型,List a = new ArrayList();,再加上SuppressWarnings注解。因为假如后面如果想要从List里面取元素出来,可能出现错误的将String元素转型为Integer,造成classCastException,或者NumberFormatException;
  2. 对于无用的方法或者类,立马删除,而不是加@SuppressWarnings("unused"),脏代码会破坏阅读体验;

标签:选项,Java,SuppressWarnings,warnings,suppress,relative,注解,警告
来源: https://blog.csdn.net/lonelymanontheway/article/details/120129912

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有