我們在寫代碼的時候,不論是導入的包,還是聲明的對象,有時候會出現黃線,感覺就很難受!
@SuppressWarnings註解主要用在取消一些編譯器產生的警告對代碼左側行列的遮擋,有時候這會擋住我們斷點調試時打的斷點。 如圖所示:
這裏,有強迫症的老鐵 就難受了!哈哈
這時候我們在方法上加上@SuppressWarnings註解就可以消除這些警告的產生,註解的使用有三種:
1. @SuppressWarnings("unchecked") [^ 抑制單類型的警告]
2. @SuppressWarnings("unchecked","rawtypes") [^ 抑制多類型的警告]
3. @SuppressWarnings("unchecked") [^ 抑制所有類型的警告]
***
> 通過源碼分析可知@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 (抑制確在switch中缺失breaks的警告) |
finally | to suppress warnings relative to finally block that don’t return (抑制finally模塊沒有返回的警告) |
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)(忽略沒有完整的switch語句) |
nls | to suppress warnings relative to non-nls string literals( 忽略非nls格式的字符) |
null | to suppress warnings relative to null analysis( 忽略對null的操作) |
rawtypes | to suppress warnings relative to un-specific types when using generics on class params( 使用generics時忽略沒有指定相應的類型) |
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( 忽略在serializable類中沒有聲明serialVersionUID變量) |
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( 抑制沒被使用過的代碼的警告) |
個人覺得比較精緻的人,應該會注意這些問題吧,個人很少管這個的!哈哈哈