@SuppressWarnings使用的正确姿势
生活随笔
收集整理的這篇文章主要介紹了
@SuppressWarnings使用的正确姿势
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
@SuppressWarnings比較常見,理解和使用起來都很簡單。
但是就這這個(gè)機(jī)會系統(tǒng)的整理一下。
通過源碼可以看出,支持在類、屬性、方法、參數(shù)、構(gòu)造方法、本地變量上使用。
@SuppressWarnings注解的使用有三種:
下面一個(gè)簡單的例子:
public class Machine {private List versions;public void addVersion(String version) {versions.add(version);} }編譯器會發(fā)出使用原始數(shù)據(jù)類型的警告
如果不想修復(fù)這個(gè)問題,可以通過@SuppressWarnings注解來抑制編譯器的警告。
該注解允許指定忽略該方法的哪些特定警告。
不同的編譯器或IDE的警告類型可能會不同,最常見的兩種類型是deprecation和unchecked。
deprecation 告訴編譯器忽略過時(shí)的方法或類型檢查。
unchecked 告訴編譯器忽略原始類型檢查
如果想抑制多種類型的警告,可以采用字符串?dāng)?shù)組方式
@SuppressWarnings({"unchecked", "deprecated"})Eclipse Galileo版本支持的抑制警告的名稱:
| all | to suppress all warnings (抑制所有警告) |
| boxing | to suppress warnings relative to boxing/unboxing operations (抑制裝箱、拆箱操作時(shí)候的警告) |
| cast | to suppress warnings relative to cast operations (抑制映射相關(guān)的警告) |
| 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時(shí)忽略沒有指定相應(yīng)的類型) |
| 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( 抑制不正確的靜態(tài)訪問方式警告) |
| synthetic-access | to suppress warnings relative to unoptimized access from inner classes( 抑制子類沒有按最優(yōu)方法訪問內(nèi)部類的警告) |
| unchecked | to suppress warnings relative to unchecked operations( 抑制沒有進(jìn)行類型檢查操作的警告) |
| unqualified-field-access | to suppress warnings relative to field access unqualified( 抑制沒有權(quán)限訪問的域的警告) |
| unused | to suppress warnings relative to unused code( 抑制沒被使用過的代碼的警告) |
參考文章:
https://www.baeldung.com/java-suppresswarnings
https://www.cnblogs.com/perfei456/p/8962167.html
總結(jié)
以上是生活随笔為你收集整理的@SuppressWarnings使用的正确姿势的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis单线程原理___Redis为何
- 下一篇: 共建智慧海关 第四范式打造快、准、稳海关