您现在的位置是:首页 > 文章详情

java注解用法详解——@SuppressWarnings

日期:2018-05-10点击:641

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 黑泽明军
【转载文章务必保留出处和署名,谢谢!】
原文链接:https://yq.aliyun.com/articles/607448
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章