java标记过期方法

2019-11-09 19:16:35

ava注解:@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


  • 2017-04-06 15:11:03

    浅析php-fpm 和 mysql 之间的关系详解

    php-fpm 和 mysql 之间的关系估计不做底层开发应用的是不会去考虑分析它们了,如果是的话我们来看一篇关于php-fpm 和 mysql 之间的关系的教程。

  • 2017-04-06 15:15:16

    PHP-FPM配置及使用总结

    PHP-FPM配置及使用总结: PHP-FPM是一个PHP FastCGI的管理器,它实际上就是PHP源代码的补丁,旨在将FastCGI进程管理引进到PHP软件包中,我们必须将其patch到PHP源代码中,然后再行编译才能使用。而现在我们可以在PHP 5.3.2及更新版本中直接开启并使用即可,因为PHP从该版本已经将其收入到软件包中,所以其不再是补丁包的存在了。

  • 2017-04-07 23:18:29

    mac 安装composer

    当你下载了 composer.phar 后,可以将它放在目录中,但每次当你建立新目录时,你必須再复制一个副本到新目录中,这样比较麻烦。所以最佳做法是将它放到 usr/local/bin 目录中中,成为全域指令。

  • 2017-04-12 10:52:35

    最全面的Android Webview详解

    现在很多App里都内置了Web网页(Hyprid App),比如说很多电商平台,淘宝、京东、聚划算等等,如下图

  • 2017-04-12 11:22:43

    WebView你真的熟悉吗?看了才知道

    在Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装为一个叫做WebView组件。下面总结一下使用webview遇到的那些事、那些坑。

  • 2017-04-12 11:28:14

    史上最全webview详解

    WebView在现在的项目中使用的频率应该还是非常高的。 我个人总觉得HTML5是一种趋势。找了一些东西,在此总结。 本篇最后有一个非常不错 的 Html5Activity 加载类,不想看的可以直接跳下载。