Java魔法堂:註解用法詳解——@SuppressWarnings

來源:互聯網
上載者:User

標籤:

一、前言                                  

  編碼時我們總會發現如下變數未被使用的警告提示:

  上述代碼編譯通過且可以運行,但每行前面的“驚嘆號”就嚴重阻礙了我們判斷該行是否設定的斷點了。這時我們可以在方法前添加 @SuppressWarnings("unused") 去除這些“驚嘆號”。

 

二、 @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 的源碼可知,其註解目標為類、欄位、函數、函數入參、建構函式和函數的局部變數。

 而家建議註解應聲明在最接近警告發生的位置。

 

四、抑制警告的關鍵字                                

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

 

五、Java Lint選項                            

  1. lint的含義

  用於在編譯器的過程中,進行更細節的額外檢查。

  2.  javac 的標準選項和非標準選項

     標準選項:是指目前的版本和未來版本中都支援的選項,如 -cp 和 -d 等。

     非標準選項:是指目前的版本支援,但未來不一定支援的選項。通過 javac -X 查看目前的版本支援的非標準選項。

  3. 查看警告資訊

   預設情況下執行 javac 僅僅顯示警告的扼要資訊,也不過阻止編譯過程。若想查看警告的詳細資料,則需要執行 javac -Xlint:keyword 來編譯源碼了。

 

六、總結                                  

  現在再都不怕不知道設定斷點沒有咯!

  尊重原創,轉載請註明來自:http://www.cnblogs.com/fsjohnhuang/p/4040785.html  ^_^肥仔John

 

七、參考                                  

http://blog.csdn.net/mddy2001/article/details/8291484

http://www.cnblogs.com/liubiqu/archive/2008/06/01/1211503.html

http://www.360doc.com/content/13/0913/17/1171_314224417.shtml

Java魔法堂:註解用法詳解——@SuppressWarnings

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.