@SuppressWarnings忽略警告


簡介:java.lang.SuppressWarnings是J2SE 5.0中標准的Annotation之一。可以標注在類、字段、方法、參數、構造方法,以及局部變量上。作用:告訴編譯器忽略指定的警告,不用在編譯完成后出現警告信息。
使用:
@SuppressWarnings(“”)
@SuppressWarnings({})
@SuppressWarnings(value={})

根據sun的官方文檔描述:
value - 將由編譯器在注釋的元素中取消顯示的警告集。允許使用重復的名稱。忽略第二個和后面出現的名稱。出現未被識別的警告名不是 錯誤:編譯器必須忽略無法識別的所有警告名。但如果某個注釋包含未被識別的警告名,那么編譯器可以隨意發出一個警告。

各編譯器供應商應該將它們所支持的警告名連同注釋類型一起記錄。鼓勵各供應商之間相互合作,確保在多個編譯器中使用相同的名稱。

示例:

·   @SuppressWarnings("unchecked")

  告訴編譯器忽略 unchecked 警告信息,如使用List,ArrayList等未進行參數化產生的警告信息。

·   @SuppressWarnings("serial")

  如果編譯器出現這樣的警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long

 使用這個注釋將警告信息去掉。

·   @SuppressWarnings("deprecation")

  如果使用了使用@Deprecated注釋的方法,編譯器將出現警告信息。
 使用這個注釋將警告信息去掉。

·   @SuppressWarnings("unchecked", "deprecation")

  告訴編譯器同時忽略unchecked和deprecation的警告信息。

·   @SuppressWarnings(value={"unchecked", "deprecation"})

  等同於@SuppressWarnings("unchecked", "deprecation")

1、抑制單類型警告

1 @SuppressWarnings("unchecked")
2 public void addItems(String item){
3   @SuppressWarnings("rawtypes")
4    List items = new ArrayList();
5    items.add(item);
6 }

2、抑制多類型警告

@SuppressWarnings(value={"unchecked", "rawtypes"})
public void addItems(String item){
   List items = new ArrayList();
   items.add(item);
}

3、抑制全部警告

1 @SuppressWarnings("all")
2 public void addItems(String item){
3    List items = new ArrayList();
4    items.add(item);
5 }

注解目標                                

 通過 @SuppressWarnings 的源碼可知,其注解目標為類、字段、函數、函數入參、構造函數和函數的局部變量。而家建議注解應聲明在最接近警告發生的位置。

抑制警告的關鍵字                                

 

關鍵字 用途
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM