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


  • 2019-03-13 09:38:11

    写入MySQL报错超出 max_allowed_packet 的问题

    MySQL会根据配置文件会限制server接受的数据包的大小。如果写入大数据时,因为默认的配置太小,插入和更新操作会因为 max_allowed_packet 参数限制,而导致失败。

  • 2019-03-15 15:28:33

    10分钟教你搭建自己的ngrok服务器

    内网穿透想必开发过微信的同志都很了解,大部分人选择网上寻找各种现成的,比如ngrok官网、ittun-ngrok、sunny-ngrok或者花生壳之类的。但是世界上没有免费的午餐,要不就是收费,要不就是免费但是偶尔会出现连接失败的问题(当然大多数时间是没有问题的)。

  • 2019-03-15 15:29:53

    丢弃花生壳,搭建自己的ngrok作为内网穿透服务器

    公司没有公网,公司在二级路由下面(就是服务商没有给独立IP,也无法动态获取公网IP),然而公司在开发程序的时候,如对接微信等需要返回数据,或者需要别人能访问我们网址,一般我们就只能购买花生壳内网穿透,但是在使用中发现速度慢,经常掉。所以搭建自己的ngrok服务器来保证内网穿透。

  • 2019-03-15 15:31:21

    内网穿透 ngrok 服务器和客户端配置

    ngrok 简介及作用 ngrok 是一款用 go 语言开发的开源软件,它是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道。下图简述了 ngrok 的原理。

  • 2019-03-15 15:32:09

    内网打洞以及代码实现

    假设内网的多个ip或者同一ip的不同port,都要访问同一个(外网ip:port)。对NAT来说,从外网接收的包它的(srcIp:srcPort)==(serverIp,serverPort),它的dstIp==natIp,所以NAT只能用dstPort来决定把这个包转发给哪一个(内网Ip:port)

  • 2019-03-15 15:33:08

    Xshell不能按退格、删除键的解决方案

    在使用xshell时,由于每个服务器不同,一些无法使用Backspace键向后删除字符。针对这个问题,本文为大家解答下退格键无法识别如何设置?

  • 2019-03-15 15:49:28

    win10远程桌面连接不上解决方法

    有朋友就感叹电脑的世界真的是很神奇,可以将整个世界连接在一起。如果别人想要摆弄你的电脑,即使不在一个地方也可以利用远程桌面来控制。而这就是所谓的远程控制操作了,大部分人都知道它的作用,不过这也不排除会遇到一些突发情况的时候,例如win10远程桌面连接不上,这该怎么去解决呢?为此,小编给大家带来了解决的图文操作。