java注解:@Deprecated(不建议使用的,废弃的);@SuppressWarnings(忽略警告,达到抑制编译器产生警告的目的)

java注解:@Deprecated(不建议使用的,废弃的), @SuppressWarnings(忽略警告,达到抑制编译器产生警告的目的)
@Deprecated可以修饰类、方法、变量,在java源码中被@Deprecated修饰的类、方法、变量等表示不建议使用的,可能会出现错误的,可能以后会被删除的类、方法等,如果现在使用,则在以后使用了这些类、方法的程序在更新新的JDK、jar包等就会出错,不再提供支持。     个人程序中的类、方法、变量用@Deprecated修饰同样是不希望自己和别人在以后的时间再次使用此类、方法。  当编译器编译时遇到了使用@Deprecated修饰的类、方法、变量时会提示相应的警告信息。
@SuppressWarnings 可以达到抑制编译器编译时产生警告的目的,但是很不建议使用@SuppressWarnings注解,使用此注解,编码人员看不到编译时编译器提示的相应的警告,不能选择更好、更新的类、方法或者不能编写更规范的编码。同时后期更新JDK、jar包等源码时,使用@SuppressWarnings注解的代码可能受新的JDK、jar包代码的支持,出现错误,仍然需要修改。
可以看成@Deprecated注解和@SuppressWarnings注解是成对出现的。

通过@SuppressWarnings的源码可知,其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。而家建议注解应声明在最接近警告发生的位置


[java]  view plain  copy
  1.    /**  
  2. * @Description: 编码时我们总会发现如下变量未被使用的警告提示, 
  3. * 上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了。 
  4. * 这时我们可以在方法前添加 @SuppressWarnings("unchecked") 去除这些“感叹号”。 
  5. */  
  6. public String convertXmlToSendMethods(Document document){  
  7.     //发布方式(1到多个)  
  8.     List<Element> methodName = document.selectNodes("/alert/code/method/methodName");  
  9.     List<Element> message = document.selectNodes("/alert/code/method/message");  
  10.     List<Element> audienceGrp = document.selectNodes("/alert/code/method/audienceGrp");  
  11.     List<Element> audenceprt = document.selectNodes("/alert/code/method/audenceprt");  
  12.     //处理‘发布方式’复数  
  13.     int methodNameSize = 0;  
  14.     if (null!=methodName && methodName.size()>0) {  
  15.         methodNameSize = methodName.size();  
  16.     }  
  17.     JSONArray jsonArraySendMethods = new JSONArray();  
  18.     for (int i = 0; i < methodNameSize; i++) {  
  19.         JSONObject json = new JSONObject();  
  20.         json.put("methodName", methodName.get(i).getTextTrim());  
  21.         json.put("message", message.get(i).getTextTrim());  
  22.         json.put("audienceGrp", audienceGrp.get(i).getTextTrim());  
  23.         json.put("audenceprt", audenceprt.get(i).getTextTrim());  
  24.         jsonArraySendMethods.put(json);  
  25.     }  
  26.     return jsonArraySendMethods.toString();  
  27. }  

@SuppressWarings注解 详解

[java]  view plain  copy
  1.    /**  
  2. * @Description:示例1——抑制单类型的警告:  
  3. */  
  4. @SuppressWarnings("unchecked")  
  5. public void addItems(String item){  
  6.   @SuppressWarnings("rawtypes")  
  7.    List items = new ArrayList();  
  8.    items.add(item);  
  9. }  
  10.   
  11. /**  
  12. * @Description: 示例2——抑制多类型的警告:  
  13. */  
  14. @SuppressWarnings(value={"unchecked""rawtypes"})  
  15. public void addItems(String item){  
  16.    List items = new ArrayList();  
  17.    items.add(item);  
  18. }  
  19.   
  20. /**  
  21. * @Description:示例3——抑制所有类型的警告: 
  22. */  
  23. @SuppressWarnings("all")  
  24. public void addItems(String item){  
  25.    List items = new ArrayList();  
  26.    items.add(item);  
  27. }  
抑制警告的关键字
关键字 用途
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

猜你喜欢

转载自blog.csdn.net/qq_15037231/article/details/79991265
今日推荐