@SuppressWarnings
http://www.cnblogs.com/fsjohnhuang/p/4040785.html
一、前言
编码时我们总会发现如下变量未被使用的警告提示:

上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了。这时我们可以在方法前添加 @SuppressWarnings("unused") 去除这些“感叹号”。
二、 @SuppressWarings注解
作用:用于抑制编译器产生警告信息。
示例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的更多相关文章
- @SuppressWarnings注解的用法
一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的"感叹号"就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @S ...
- @SuppressWarnings的参数
@SuppressWarnings 是J2EE的最后一个批注,该批注的作用是告诉编译器对被批注的元素内部的某些警告保持静默 @SuppressWarnings("unchecked" ...
- @SuppressWarnings("finally")
@SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. 批注允许您选择性地取消特定代码段(即,类或方法)中的警告.其中的想法是当您看到 ...
- @SuppressWarnings("deprecation")
在Java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings("XXXX") ...
- @SuppressWarnings忽略警告
简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...
- @SuppressWarnings的使用、作用、用法
在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决 例如:@S ...
- @SuppressWarnings有什么用处?
J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择 ...
- Java注释Override、Deprecated、SuppressWarnings详解(过时方法,即将删除的方法或成员变量)
Override 这个注释的作用是标识某一个方法是否覆盖了它的父类的方法.那么为什么要标识呢?让我们来看看如果不用Override标识会发生什么事情. Deprecated 这个注释是一个标记注释.所 ...
- Java注释Override、Deprecated、SuppressWarnings详解
一.什么是注释 说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J2SE5 ...
- Java魔法堂:注解用法详解——@SuppressWarnings
一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @SuppressWar ...
随机推荐
- Thrift入门及Java实例演示
目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...
- DOS命令下输入:java Hello 出现以下几种结果可能的原因:
DOS命令下输入:java Hello 出现以下结果:Bad command or the file name 没有这个命令或文件名 原因可能是没有成功安装jdk或者没有配置好jdk 的环境变量,或者 ...
- 连连看的设计与实现——四人小组项目(GUI)
项目GUI界面经过简单设计,整理如下:(图片截取致宠物连连看3.1) 点开游戏后界面显示: 点击菜单游戏—>初级 后显示 -------------------------- > 当游戏时 ...
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4893 Accepted: 2271 ...
- Javascript 笔记与总结(2-8)对象2
注意:标签属性与 DOM 对象属性的对应关系,绝大部分 2 者是相同的,例如 imgobj.src 属性对应 <img src=""> 中的 src 属性 例外:< ...
- EditText小技巧
1.让EditText不自动获取焦点 将EditText的某个父级控件设置成 android:focusable="true" android:focusableInTouchMo ...
- typecho流程原理和插件机制浅析(第二弹)
typecho流程原理和插件机制浅析(第二弹) 兜兜 393 2014年04月02日 发布 推荐 1 推荐 收藏 14 收藏,3.7k 浏览 上一次说了 Typecho 大致的流程,今天简单说一下插件 ...
- NFS文件系统
一.nfs服务端CentOS6安装nfs-utils和rpcbind两个软件包yum install nfs-utils rpcbind编辑/etc/exports加入:/nfsdir 192.168 ...
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- Java 常见问题思考
Java中的成员变量是选择在声明时赋值还是在构造函数中赋值? // 通过构造函数赋值 public class UserRequestImpl extends ModuleImplBase { pri ...