@SuppressWarnings:解决IDE中难看的黄色标识

1,836 阅读3分钟

背景

 同事得了急性阑尾炎,卧床不起,帮他开发项目,发现他在类上总加上 @SuppressWarnings("all") 。乍一看挺熟悉,在Spring和一些开源框架源码中经常见到,加上之后最直观的表现就是代码中拼写不完整的单词没有黄色波浪线了。带着疑问,决定多了解一下...

认知历程

 SuppressWarning中Suppress是啥意思,英语不好不得已百度翻译下,suppress:抑制。哦,顾名思义,抑制警告。看了下源码注释,讲了下怎么用。这注解可以用于几乎任何地方,类,方法,字段,参数等等。源码如下:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings{
    String[] value();
}

 建议哪里报warning,用在哪里,别只在最外层类上加上。这么看,我的同事是偷懒了。最外层的@SuppressWarning注解和内层的@SuppressWarning的注解会共同作用域内层@SuppressWarning注解的target。源码注释如下:

/**
 * Indicates that the named compiler warnings should be suppressed in the
 * annotated element (and in all program elements contained in the annotated
 * element).  Note that the set of warnings suppressed in a given element is
 * a superset of the warnings suppressed in all containing elements.  For
 * example, if you annotate a class to suppress one warning and annotate a
 * method to suppress another, both warnings will be suppressed in the method.
 *
 * <p>As a matter of style, programmers should always use this annotation
 * on the most deeply nested element where it is effective.  If you want to
 * suppress a warning in a particular method, you should annotate that
 * method rather than its class.
 **/

 那么,怎么用呢,网上搜索了一下,括号里面填相应的值就好了,例如:处理所有的警告:@SuppressWarnings("all");或者处理过期的方法@SuppressWarnings("deprecation");亦或者处理非检查异常的代码@SuppressWarnings("unchecked"),代码示例:

public class SuppressWarningDemo {
    @SuppressWarnings("unchecked")
    public void addItem(String item) {
        List items = new ArrayList();
        items.add(item);
        @SuppressWarnings("unused")
        int i = 0;
    }
}

 也可以这么写:

public class SuppressWarningDemo {
    @SuppressWarnings(value = {"unchecked", "unused"})
    public void addItem(String item) {
        List items = new ArrayList();
        items.add(item);
        int i = 0;
    }
}

当然,如果觉得还想懒一点就学学我同事吧:

@SuppressWarnings("all")
public class SuppressWarningDemo {
    public void addItem(String item) {
        List items = new ArrayList();
        items.add(item);
        int i = 0;
    }
}

 比较好奇value的值是在哪里定义得,源码里没找到,jdk文档里也没有,如果有大佬路过,正巧又知道,烦请留言告知一下。网上列举了一些常用的value值,管他实用不实用,我就抄一遍:

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				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

总结

 第一天写日常总结,额,写的很糟糕,不过,也算是一个好的开始的,坚持输出,加深对知识点的印象,也是在连接世界。欢迎走过路过的朋友指点一二。