作用:告訴編譯器忽略指定的警告,不用在編譯完成后出現警告信息。
使用:
@SuppressWarnings(“”)
@SuppressWarnings({})
@SuppressWarnings(value={})
根據sun的官方文檔描述:
value -將由編譯器在注釋的元素中取消顯示的警告集。允許使用重復的名稱。忽略第二個和后面出現的名稱。出現未被識別的警告名 不是錯誤:編譯器必須忽略無法識別的所有警告名。但如果某個注釋包含未被識別的警告名,那么編譯器可以隨意發出一個警告。
使用這個注釋將警告信息去掉。
使用這個注釋將警告信息去掉。
轉自http://blog.sina.com.cn/s/blog_ad8b5870010166vt.html
編碼時我們總會發現如下變量未被使用的警告提示:
上述代碼編譯通過且可以運行,但每行前面的“感嘆號”就嚴重阻礙了我們判斷該行是否設置的斷點了。這時我們可以在方法前添加 @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