java最后一个注解@SuppressWarnings
簡(jiǎn)介:java.lang.SuppressWarnings是J2SE5.0中標(biāo)準(zhǔn)的Annotation之一。可以標(biāo)注在類、字段、方法、參數(shù)、構(gòu)造方法,以及局部變量上。 作用:告訴編譯器忽略指定的警告,不用在編譯完成后出現(xiàn)警告信息。 使用: @SuppressWarnings(“”) @SuppressWarnings({}) @SuppressWarnings(value={})
根據(jù)sun的官方文檔描述: value -將由編譯器在注釋的元素中取消顯示的警告集。允許使用重復(fù)的名稱。忽略第二個(gè)和后面出現(xiàn)的名稱。出現(xiàn)未被識(shí)別的警告名不是錯(cuò)誤:編譯器必須忽略無(wú)法識(shí)別的所有警告名。但如果某個(gè)注釋包含未被識(shí)別的警告名,那么編譯器可以隨意發(fā)出一個(gè)警告。 各編譯器供應(yīng)商應(yīng)該將它們所支持的警告名連同注釋類型一起記錄。鼓勵(lì)各供應(yīng)商之間相互合作,確保在多個(gè)編譯器中使用相同的名稱。 示例: ·??@SuppressWarnings("unchecked") 告訴編譯器忽略 unchecked 警告信息,如使用List,ArrayList等未進(jìn)行參數(shù)化產(chǎn)生的警告信息。 ·??@SuppressWarnings("serial") 如果編譯器出現(xiàn)這樣的警告信息:The serializable class WmailCalendar does notdeclare 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")
? 編碼時(shí)我們總會(huì)發(fā)現(xiàn)如下變量未被使用的警告提示:
? 上述代碼編譯通過(guò)且可以運(yùn)行,但每行前面的“感嘆號(hào)”就嚴(yán)重阻礙了我們判斷該行是否設(shè)置的斷點(diǎn)了。這時(shí)我們可以在方法前添加?@SuppressWarnings("unused")?去除這些“感嘆號(hào)”。
?
一、?@SuppressWarings注解?
? 作用:用于抑制編譯器產(chǎn)生警告信息。
? 示例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); }?
二、注解目標(biāo)
?通過(guò)?@SuppressWarnings?的源碼可知,其注解目標(biāo)為類、字段、函數(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 |
| 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選項(xiàng)
? 1. lint的含義
用于在編譯程序的過(guò)程中,進(jìn)行更細(xì)節(jié)的額外檢查。
? 2.??javac?的標(biāo)準(zhǔn)選項(xiàng)和非標(biāo)準(zhǔn)選項(xiàng)
? ? ?標(biāo)準(zhǔn)選項(xiàng):是指當(dāng)前版本和未來(lái)版本中都支持的選項(xiàng),如?-cp?和?-d?等。
? ? ?非標(biāo)準(zhǔn)選項(xiàng):是指當(dāng)前版本支持,但未來(lái)不一定支持的選項(xiàng)。通過(guò)?javac -X?查看當(dāng)前版本支持的非標(biāo)準(zhǔn)選項(xiàng)。
? 3. 查看警告信息
? ?默認(rèn)情況下執(zhí)行?javac?僅僅顯示警告的扼要信息,也不過(guò)阻止編譯過(guò)程。若想查看警告的詳細(xì)信息,則需要執(zhí)行?javac -Xlint:keyword?來(lái)編譯源碼了。
?
五、總結(jié)
? 現(xiàn)在再都不怕不知道設(shè)置斷點(diǎn)沒(méi)有咯!
總結(jié)
以上是生活随笔為你收集整理的java最后一个注解@SuppressWarnings的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringMVC级联获取前台数据
- 下一篇: 漫画说算法--动态规划算法三(绝对通俗易