Java @SuppressWarnings 注解使用

    本文参考http://www.cnblogs.com/fsjohnhuang/p/4040785.html,如有侵权,请联系我删除。

    我们在开发的时候经常会遇到编译器报的警告信息,比如你新建了一个变量,但是这个变量没有被引用,就会在 Eclipse 编译器的左侧显示一个黄色感叹号(对,就是打断点的地方),鼠标移动上去,就会浮现“The value of the local variable test is not used”,非常讨厌,都不知道断点打没打,而且喜欢用// TODO和// FIXME的人也会因为看不清右侧蓝色块而烦恼,再比如你引用了一些过时的方法(就是方法上有删除线),也会报“The method xxx() from the type Xxx is deprecated”,再比如类型转换的问题:“Type safety: Unchecked cast from Object to List<Xxx>”,还好有这个注解,可以在开发的时候大大减少一些麻烦事。

    以下是 @SuppressWarnings 注解的源码:

/*
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;

/**
 * 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.
 *
 * @since 1.5
 * @author Josh Bloch
 */
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p>Compiler vendors should document the warning names they support in
     * conjunction with this annotation type. They are encouraged to cooperate
     * to ensure that the same names work across multiple compilers.
     */
    String[] value();
}

    可以看到 @SuppressWarnings 所针对的范围很全面,类,属性/变量,方法,参数,构造方法,局部变量,都可以使用,(FIELD这里我个人的理解是属性/变量,希望如果我错了,可以给我指正),以下是 @SuppressWarnings 注解的一些用法:

 

一、用法

1. 抑制单类型警告

@SuppressWarnings("unused")// 抑制没有使用变量的警告
String str = "";

 

2. 抑制多类型警告

@SuppressWarnings(value={"unchecked", "unused"})// 抑制强制转换不安全和没有使用变量的警告
public void execute() {
	List<Xxx> list = (List<Xxx>) dataMap.get("xxxList");
	String str = "";
}

 

3. 抑制所有警告

@SuppressWarnings("all")// 抑制所有警告
String str = "";

 

二、关键字

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

转载于:https://my.oschina.net/NamiZone/blog/1523038

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值