java注解用法详解——@SuppressWarnings
java中注解用法详解——@SuppressWarnings
一、前言
注释类型: 当你的编码可能存在警告时,比如安全警告,可以用它来消除。 api中是这样描述的: 指示应该在注释元素(以及包含在该注释元素中的所有程序元素)中取消显示指定的编译器警告。 注意,在给定元素中取消显示的警告集是所有包含元素中取消显示的警告的超集。 例如,如果注释一个类来取消显示某个警告,同时注释一个方法来取消显示另一个警告,那么将在此方法中同时取消显示这两个警告。 根据风格不同,程序员应该始终在最里层的嵌套元素上使用此注释,在那里使用才有效。 如果要在特定的方法中取消显示某个警告,则应该注释该方法而不是注释它的类。
在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决
例如:@SuppressWarnings("deprecation") 表示不显示使用了不赞成使用的类或方法时的警告。
再比如:
编码时我们总会发现如下:变量未被使用的警告提示
上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了。这时我们可以在方法前添加 @SuppressWarnings("unused") 去除这些“感叹号”。
再比如:
这时我们可以在方法前添加 @SuppressWarnings("resource") 去除这些“感叹号”。
二、 @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 的源码可知,其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。
而专家建议注解应声明在最接近警告发生的位置。
四、抑制警告的关键字
It depends on your IDE or compiler.
Here is a list for Eclipse Galileo:
- 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
- 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
List for Indigo adds:
- javadoc to suppress warnings relative to javadoc warnings
- rawtypes to suppress warnings relative to usage of raw types
- static-method to suppress warnings relative to methods that could be declared as static
- super to suppress warnings relative to overriding a method without super invocations
List for Juno adds:
- resource to suppress warnings relative to usage of resources of type Closeable
- sync-override to suppress warnings because of missing synchronize when overriding a synchronized method
部分翻译如下:
@SuppressWarnings(“unchecked”) // 抑制未检查的转化,例如集合没有指定类型的警告
@SuppressWarnings(“unused”) // 抑制未使用的变量的警告
@SuppressWarnings(“resource”) // 抑制与使用Closeable类型资源相关的警告
@SuppressWarnings(“path”) // 抑制在类路径,原文件路径中有不存在的路径的警告
@SuppressWarnings("deprecation") // 抑制使用了某些不赞成使用的类和方法的警告
@SuppressWarnings("fallthrough") // 抑制switch语句执行到底没有break关键字的警告
@SuppressWarnings("serial") // 抑制某类实现Serializable,但是没有定义serialVersionUID,这个需要但是不必须的字段的警告
@SuppressWarnings("rawtypes") // 抑制没有传递带有泛型的参数的警告
@SuppressWarnings("all") // 抑制全部类型的警告
我的GitHub地址: https://github.com/heizemingjun
我的博客园地址: http://www.cnblogs.com/chenmingjun
我的蚂蚁笔记博客地址: http://blog.leanote.com/chenmingjun
Copyright ©2018 黑泽明军
【转载文章务必保留出处和署名,谢谢!】
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
SpringFramework核心技术一(IOC:使用ICO容器)
使用容器 这ApplicationContext是高级工厂的接口,能够维护不同bean及其依赖项的注册表。使用该方法T getBean(String name, Class requiredType)可以检索bean的实例。 一、 在ApplicationContext可以读取bean定义并访问它们,如下所示: 1.1 怎么使用容器 首先来看一下容器: 1.2 有很多同学一上来就是搞一个New,可是这是一个ApplicationContext接口啊。 //接口是不能直接new着用的,这个是重写。 ApplicationContext applicationContext = new ApplicationContext(){...} 1.3 下面这样用才是对的:ClassPathXmlApplicationContext是类 /** * @author:wangdong * @description:怎么使用容器 */ public class Test { public static void main(String[] args) { // create and configure...
- 下一篇
phinx数据库脚本迁移工具
phinx数据库脚本迁移工具 Phinx 可以使用 Composer 进行安装,Composer是一个PHP依赖管理工具。更多信息请访问 Composer 官网。 Phinx 至少需要PHP 5.4 或更新的版本 第一步:安装 composer require robmorgan/phinx 第二步:初始化 安装后,Phinx 现在可以在你的项目中执行初始化 php vendor/robmorgan/phinx/bin/phinx init 第三步:配置文件 phinx.yml 第四步:创建迁移 文件名驼峰命名 php vendor/robmorgan/phinx/bin/phinx create MyNewMigration 这将创建一个新的迁移脚本,格式是 YYYYMMDDHHMMSS_my_new_migration.php ,前14个字符是当前的timestamp,精确到秒。 如果你指定了多个脚本路径,将会提示你选择哪一个。 Phinx 自动创建的迁移脚本框架有一个方法: <?php use Phinx\Migration\AbstractMigration; class...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Mario游戏-低调大师作品
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2整合Thymeleaf,官方推荐html解决方案