简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,以及局部变量上。

作用:告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。
使用:
@SuppressWarnings(“”)
@SuppressWarnings({})
@SuppressWarnings(value={})

根据sun的官方文档描述:
value - 将由编译器在注释的元素中取消显示的警告集。允许使用重复的名称。忽略第二个和后面出现的名称。出现未被识别的警告名不是 错误:编译器必须忽略无法识别的所有警告名。但如果某个注释包含未被识别的警告名,那么编译器可以随意发出一个警告。

各编译器供应商应该将它们所支持的警告名连同注释类型一起记录。鼓励各供应商之间相互合作,确保在多个编译器中使用相同的名称。

示例:

·   @SuppressWarnings("unchecked")

告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。

·   @SuppressWarnings("serial")

如果编译器出现这样的警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long

使用这个注释将警告信息去掉。

·   @SuppressWarnings("deprecation")

如果使用了使用@Deprecated注释的方法,编译器将出现警告信息。
 使用这个注释将警告信息去掉。

·   @SuppressWarnings("unchecked", "deprecation")

告诉编译器同时忽略unchecked和deprecation的警告信息。

·   @SuppressWarnings(value={"unchecked", "deprecation"})

等同于@SuppressWarnings("unchecked", "deprecation")

1、抑制单类型警告

1 @SuppressWarnings("unchecked")
2 public void addItems(String item){
3   @SuppressWarnings("rawtypes")
4    List items = new ArrayList();
5    items.add(item);
6 }

2、抑制多类型警告

@SuppressWarnings(value={"unchecked", "rawtypes"})
public void addItems(String item){List items = new ArrayList();items.add(item);
}

3、抑制全部警告

1 @SuppressWarnings("all")
2 public void addItems(String item){
3    List items = new ArrayList();
4    items.add(item);
5 }

注解目标                                

通过 @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

@SuppressWarnings忽略警告相关推荐

  1. java注解:@Deprecated(不建议使用的,废弃的);@SuppressWarnings(忽略警告,达到抑制编译器产生警告的目的)

    java注解:@Deprecated(不建议使用的,废弃的), @SuppressWarnings(忽略警告,达到抑制编译器产生警告的目的) @Deprecated可以修饰类.方法.变量,在java源 ...

  2. 忽略警告注解@SuppressWarnings详解

    忽略警告注解@SuppressWarnings详解 简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方 ...

  3. python运行不了、显示警告_Python中偶尔遇到的细节疑问(二):UnicodeDecodeError,警告与忽略警告warnings...

    1. 使用base64解码时,出现:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 21: invalid c ...

  4. 使用numpy出现DeprecationWarning: The normed argument is ignored when density is provided. 解决方法忽略警告的方法

    目录 1.从根本上进行解决 2.直接利用warning模块忽略警告 1.从根本上进行解决 "DeprecationWarning: The normed argument is ignore ...

  5. VC 忽略警告的方法

    在vs2003, vs2005中用sprintf 会出现warning C4996: 'sprintf' was declared deprecated或 warning C4996: 'strcpy ...

  6. iOS常用的忽略警告

    在iOS开发过程中,偶尔会碰到一些编译器警告,如果能够确定该警告不会影响到程序的正常运行,则可以手动告诉编译器忽略掉这个警告 iOS常用的忽略警告类型: 1.方法弃用警告 #pragma clang ...

  7. Xcode 项目忽略警告

    对于一个有强迫症的我,每次 ⌘B Build 项目时,发现一个警告都要点进去修复了,然而,对于一些无关紧要的警告,我们是否可以选择忽略这个警告,让 Xcode 不提示呢?答案当然是可以的. 下面,就介 ...

  8. BUILD SUCCESSFUL有警告无法运行,需忽略警告

    1.在app 的build.gradle中 添加 lintOptions { checkReleaseBuilds false abortOnError false } 2. 在project的bui ...

  9. python 忽略错误,python忽略警告(warning) 错误实现方法

    这篇文章主要为大家详细介绍了python忽略警告(warning) 错误实现方法,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来 ...

最新文章

  1. 软件:向工业互联网产业成功转型的关键
  2. jquery 分片上传php,php 大文件分片上传
  3. windows文件中的中文在ubuntu下乱码(小弟参考了许多都不行,这个绝对行啊) .
  4. wps居中对齐不在中间_WPS文字快捷键总结(Windows版本)--值得收藏
  5. 荣耀30会不会升级鸿蒙,荣耀手机不能升级鸿蒙吗?有博主给出升级名单
  6. 数学倒底有没有绝对的严格性和形式化?
  7. Cisco Equipment Configuration SSH login
  8. 【笔记】shell下的主要工具
  9. 学生选课管理信息系统
  10. LTE的基础知识与关键技术
  11. Android6.0 源码修改之屏蔽系统短信功能和来电功能
  12. easypoi 批量导出_浅谈easypoi快速实现excel批量导入
  13. 分析MP4的名人博客
  14. 创建第一个mybatis程序 遇到的问题
  15. win7共享打印机设置
  16. 分享一个很好的卸载绿色软件:Geek Uninstaller(个人用户免费)
  17. 腾讯云在线WebShell终端使用体验
  18. hotmail 发送邮件 的服务器地址如下
  19. 创新奇智CTO张发恩:AI+to B还是蓝海 将诞生新巨头
  20. Linux MMC 驱动子系统详解

热门文章

  1. HCIE的考试难度和含金量,以及备考
  2. 服务器网线各线作用,涨知识 | 简单说说网线中每根线的用途
  3. 微机原理及应用实验——MOV指令的使用详解
  4. filters 传参是什么_vue过滤器filters的使用
  5. Spring Boot+Vue项目 微博系统(2):构建前端Vue项目
  6. C# 字节数限制(限制字符串字节数)
  7. vue的.then()方法
  8. 科技赋能“听觉”,和乐电子QCY在环球消费电子展上释放更多未来
  9. presto使用初探
  10. 手机html5卡,手机端js和html5刮刮卡效果