java注解:@Deprecated(不建議使用的,廢棄的), @SuppressWarnings(忽略警告,達到抑制編譯器產生警告的目的)
@Deprecated可以修飾類、方法、變量,在java源碼中被@Deprecated修飾的類、方法、變量等表示不建議使用的,可能會出現錯誤的,可能以后會被刪除的類、方法等,如果現在使用,則在以后使用了這些類、方法的程序在更新新的JDK、jar包等就會出錯,不再提供支持。 ? ? 個人程序中的類、方法、變量用@Deprecated修飾同樣是不希望自己和別人在以后的時間再次使用此類、方法。 ?當編譯器編譯時遇到了使用@Deprecated修飾的類、方法、變量時會提示相應的警告信息。
@SuppressWarnings?可以達到抑制編譯器編譯時產生警告的目的,但是很不建議使用@SuppressWarnings注解,使用此注解,編碼人員看不到編譯時編譯器提示的相應的警告,不能選擇更好、更新的類、方法或者不能編寫更規范的編碼。同時后期更新JDK、jar包等源碼時,使用@SuppressWarnings注解的代碼可能受新的JDK、jar包代碼的支持,出現錯誤,仍然需要修改。
可以看成@Deprecated注解和@SuppressWarnings注解是成對出現的。
通過@SuppressWarnings的源碼可知,其注解目標為類、字段、函數、函數入參、構造函數和函數的局部變量。而家建議注解應聲明在最接近警告發生的位置
- ???/**??
- *?@Description:?編碼時我們總會發現如下變量未被使用的警告提示,?
- *?上述代碼編譯通過且可以運行,但每行前面的“感嘆號”就嚴重阻礙了我們判斷該行是否設置的斷點了。?
- *?這時我們可以在方法前添加?@SuppressWarnings("unchecked")?去除這些“感嘆號”。?
- */??
- public?String?convertXmlToSendMethods(Document?document){??
- ????//發布方式(1到多個)??
- ????List<Element>?methodName?=?document.selectNodes("/alert/code/method/methodName");??
- ????List<Element>?message?=?document.selectNodes("/alert/code/method/message");??
- ????List<Element>?audienceGrp?=?document.selectNodes("/alert/code/method/audienceGrp");??
- ????List<Element>?audenceprt?=?document.selectNodes("/alert/code/method/audenceprt");??
- ????//處理‘發布方式’復數??
- ????int?methodNameSize?=?0;??
- ????if?(null!=methodName?&&?methodName.size()>0)?{??
- ????????methodNameSize?=?methodName.size();??
- ????}??
- ????JSONArray?jsonArraySendMethods?=?new?JSONArray();??
- ????for?(int?i?=?0;?i?<?methodNameSize;?i++)?{??
- ????????JSONObject?json?=?new?JSONObject();??
- ????????json.put("methodName",?methodName.get(i).getTextTrim());??
- ????????json.put("message",?message.get(i).getTextTrim());??
- ????????json.put("audienceGrp",?audienceGrp.get(i).getTextTrim());??
- ????????json.put("audenceprt",?audenceprt.get(i).getTextTrim());??
- ????????jsonArraySendMethods.put(json);??
- ????}??
- ????return?jsonArraySendMethods.toString();??
- }??
@SuppressWarings注解?詳解
- ???/**??
- *?@Description:示例1——抑制單類型的警告:??
- */??
- @SuppressWarnings("unchecked")??
- public?void?addItems(String?item){??
- ??@SuppressWarnings("rawtypes")??
- ???List?items?=?new?ArrayList();??
- ???items.add(item);??
- }??
- ??
- /**??
- *?@Description:?示例2——抑制多類型的警告:??
- */??
- @SuppressWarnings(value={"unchecked",?"rawtypes"})??
- public?void?addItems(String?item){??
- ???List?items?=?new?ArrayList();??
- ???items.add(item);??
- }??
- ??
- /**??
- *?@Description:示例3——抑制所有類型的警告:?
- */??
- @SuppressWarnings("all")??
- public?void?addItems(String?item){??
- ???List?items?=?new?ArrayList();??
- ???items.add(item);??
- }??
抑制警告的關鍵字
關鍵字 | 用途 |
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 |
參考:http://www.cnblogs.com/fsjohnhuang/p/4040785.html