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")             // 抑制全部类型的警告

原文地址:https://www.cnblogs.com/chenmingjun/p/9027111.html