简介: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、抑制单类型警告

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

2、抑制多类型警告

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

3、抑制全部警告

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

注解目标                                

通过 @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. iOS常用的忽略警告

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

  2. xcode忽略警告

    1.对整个文件使用 你的工程 -> 你的target -> Build Phases -> Compile Sources -> 搜索要忽略警告的文件名,在 Compiler ...

  3. jenkins配置findbugs失败---不要随便忽略警告!一个因为文件所有权引发的血案

    一:背景交代 这两天组长让我这边搭一个持续集成环境.梳理了需求后,因为我们的项目都是maven项目,所以我选择了jenkins+外置maven(区别于直接从jenkins里面安装)的方案.(cento ...

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

    简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上. 作用:告诉编译器忽略指定的警告 ...

  5. 忽略警告@SuppressWarnings的用法

    @SuppressWarnings注解用法 @SuppressWarnings注解主要用在取消一些编译器产生的警告对代码左侧行列的遮挡,有时候这会挡住我们断点调试时打的断点. 我们可以在方法上加上@S ...

  6. SuppressWarnings的警告

    简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...

  7. Qt使用MinGW编译,如何忽略警告

    Qt编译时经常出现以下警告: warning: unused parameter 'arg1' [-Wunused-parameter] warning: unused variable 'i' [- ...

  8. @SuppressWarnings抑制警告

    @SuppressWarnings(“XXXX”) 来抑制编译时的警告信息.参数如下: 关键字 用途 all to suppress all warnings boxing  to suppress ...

  9. Qt使用MinGW编译,如何忽略警告

    Qt编译时经常出现以下警告: warning: unused parameter 'arg1' [-Wunused-parameter] warning: unused variable 'i' [- ...

随机推荐

  1. 手拼SQL小技巧,WHERE 1=1

    由于项目要求,要手动拼接SQL,(不知道领导们怎么想的--),然后就再次回到原始时代,append(SQL). 但后面查询牵扯到动态多条件,如果是mybatis的话就直接 可以用<if>标 ...

  2. poj3728

    [描述] 有 N 城 市在一个国家,有一个且只有一个简单的路径每一对城市之间. 一个商人选择了一些路径和想赚尽可能多的钱在每个路径. 当他沿着一条路径,可以选择一个城市购买一些商品和出售他们在一个城市 ...

  3. babel 配置探究及错误解析

    前面的文章说到了react 15的一些特性,众所周知react搭配es6或者叫es2015的开发模式更加的方便快捷.不过提到es2015这个毕竟没有被浏览器广泛支持的规范,要想能够快快乐乐的应用起来, ...

  4. Winpcap安装,Cannot open include file 'pcap.h'

    VC报错 fatal error C1083: Cannot open include file: 'pcap.h': No such file or directory Winpcap是window ...

  5. Java猜拳小游戏(剪刀、石头、布)

    1.第一种实现方法,调用Random数据包,直接根据“1.2.3”输出“剪刀.石头.布”.主要用了9条输出判断语句. import java.util.Random; import java.util ...

  6. Hyper-v虚拟机文件VHDX与VHD的格式转换

    今天遇到一个坑,我在本机(windows 10)上创建的CentOS虚拟机作为docker的宿主机,部署了gitlab等容器,准备迁移到服务器上的时候,发现始终无法导入,提示必须通过Hyper-v导出 ...

  7. 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应

    转自http://blog.csdn.net/yanfeiouc2009/archive/2010/06/07/5653360.aspx 手头上有个东西要用到单文档,由于想省事,直接用VS2010做了 ...

  8. arch下的启动问题解决

    1.不知什么时候开始关机的时候会看到一串红色的文字 [lun. avril 20 09:15:34 2015] [drm:intel_uncore_check_errors [i915]] *ERRO ...

  9. 神经网络hopfield的学习

    Hopfield神经网络使用说明. 该神经网络有两个特点: 1,输出值只有0,1 2,Hopfield没有输入(input) 这里解释一下第二个特点,什么叫没有输入?因为在使用Hopfield网络的时 ...

  10. Core Audio(一)

    Core Audio APIs core audio apis是vista之后引入的,不使用与之前的windows版本:core audio apis提供访问endpoint devices,比如耳机 ...