欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中@SuppressWarnings注解用法詳解

 更新時(shí)間:2021年02月04日 09:54:58   作者:liulz  
這篇文章主要介紹了java中@SuppressWarnings注解用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

SuppressWarnings注解是jse提供的注解。作用是屏蔽一些無(wú)關(guān)緊要的警告。使開(kāi)發(fā)者能看到一些他們真正關(guān)心的警告。從而提高開(kāi)發(fā)者的效率

簡(jiǎn)介:

java.lang.SuppressWarnings是J2SE 5.0中標(biāo)準(zhǔn)的Annotation之一??梢詷?biāo)注在類(lèi)、字段、方法、參數(shù)、構(gòu)造方法,以及局部變量上。作用:告訴編譯器忽略指定的警告,不用在編譯完成后出現(xiàn)警告信息。

使用:

  • @SuppressWarnings(“”)
  • @SuppressWarnings({})
  • @SuppressWarnings(value={})

根據(jù)sun的官方文檔描述:

value - 將由編譯器在注釋的元素中取消顯示的警告集。允許使用重復(fù)的名稱(chēng)。忽略第二個(gè)和后面出現(xiàn)的名稱(chēng)。出現(xiàn)未被識(shí)別的警告名不是 錯(cuò)誤:編譯器必須忽略無(wú)法識(shí)別的所有警告名。但如果某個(gè)注釋包含未被識(shí)別的警告名,那么編譯器可以隨意發(fā)出一個(gè)警告。

各編譯器供應(yīng)商應(yīng)該將它們所支持的警告名連同注釋類(lèi)型一起記錄。鼓勵(lì)各供應(yīng)商之間相互合作,確保在多個(gè)編譯器中使用相同的名稱(chēng)。

示例:

@SuppressWarnings("unchecked")

告訴編譯器忽略 unchecked 警告信息,如使用List,ArrayList等未進(jìn)行參數(shù)化產(chǎn)生的警告信息。

@SuppressWarnings("serial")

如果編譯器出現(xiàn)這樣的警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long,使用這個(gè)注釋將警告信息去掉。

@SuppressWarnings("deprecation")

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

@SuppressWarnings("unchecked", "deprecation")

告訴編譯器同時(shí)忽略u(píng)nchecked和deprecation的警告信息。

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

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

@SuppressWarnings注解的作用

J2SE 提供的最后一個(gè)批注是 @SuppressWarnings。該批注的作用是給編譯器一條指令,告訴它對(duì)被批注的代碼元素內(nèi)部的某些警告保持靜默。
@SuppressWarnings 批注允許您選擇性地取消特定代碼段(即,類(lèi)或方法)中的警告。其中的想法是當(dāng)您看到警告時(shí),您將調(diào)查它,如果您確定它不是問(wèn)題,
您就可以添加一個(gè) @SuppressWarnings 批注,以使您不會(huì)再看到警告。雖然它聽(tīng)起來(lái)似乎會(huì)屏蔽潛在的錯(cuò)誤,但實(shí)際上它將提高代碼安全性,因?yàn)樗鼘⒎乐?br /> 您對(duì)警告無(wú)動(dòng)于衷 — 您看到的每一個(gè)警告都將值得注意。

我經(jīng)常遇到的問(wèn)題是不曉得什么時(shí)候用@SupressWarnings的什么批注好,所以做了如下整理

使用:

  • @SuppressWarnings(“”)
  • @SuppressWarnings({})
  • @SuppressWarnings(value={})

一.@SuppressWarings注解

作用:用于抑制編譯器產(chǎn)生警告信息。

示例1——抑制單類(lèi)型的警告:

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

示例2——抑制多類(lèi)型的警告:

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

示例3——抑制所有類(lèi)型的警告:

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

二.注解目標(biāo)

通過(guò)@SuppressWarnings 的源碼可知,其注解目標(biāo)為類(lèi)、字段、函數(shù)、函數(shù)入?yún)ⅰ?gòu)造函數(shù)和函數(shù)的局部變量。而大家建議注解應(yīng)聲明在最接近警告發(fā)生的位置。

三.抑制警告的關(guān)鍵字

抑制警告的關(guān)鍵字

  • 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(抑制過(guò)期方法警告)
  • 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模塊沒(méi)有返回的警告)
  • 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)(忽略沒(méi)有完整的switch語(yǔ)句)
  • nls  to suppress warnings relative to non-nls string literals(忽略非nls格式的字符)
  • null  to suppress warnings relative to null analysis(忽略對(duì)null的操作)
  • rawtypes  to suppress warnings relative to un-specific types when using generics on class params(使用generics時(shí)忽略沒(méi)有指定相應(yīng)的類(lèi)型)
  • 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類(lèi)中沒(méi)有聲明serialVersionUID變量)
  • static-access  to suppress warnings relative to incorrect static access(抑制不正確的靜態(tài)訪(fǎng)問(wèn)方式警告)
  • synthetic-access  to suppress warnings relative to unoptimized access from inner classes(抑制子類(lèi)沒(méi)有按最優(yōu)方法訪(fǎng)問(wèn)內(nèi)部類(lèi)的警告)
  • unchecked  to suppress warnings relative to unchecked operations(抑制沒(méi)有進(jìn)行類(lèi)型檢查操作的警告)
  • unqualified-field-access  to suppress warnings relative to field access unqualified (抑制沒(méi)有權(quán)限訪(fǎng)問(wèn)的域的警告)
  • unused  to suppress warnings relative to unused code  (抑制沒(méi)被使用過(guò)的代碼的警告)

到此這篇關(guān)于java中@SuppressWarnings注解用法詳解的文章就介紹到這了,更多相關(guān)java @SuppressWarnings注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論