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的源码可知,其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。而家建议注解应声明在最接近警告发生的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    /** 
    * @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注解 详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    /** 
    * @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-07-11 21:54:14

    MYSQL5.7版本sql_mode=only_full_group_by问题

    一旦开启 only_full_group_by ,感觉,group by 将变成和 distinct 一样,只能获取受到其影响的字段信息,无法和其他未受其影响的字段共存,这样,group by 的功能将变得十分狭窄了

  • 2017-07-14 13:51:58

    NodeJS连接MySQL出现Cannot enqueue Handshake after invoking quit.

    原因在于node连接上mysql后如果因网络原因丢失连接或者用户手工关闭连接后,原有的连接挂掉,需要重新连接;如下代码,每次访问结束都关闭,每次开始访问前重连接下,代码中没有监听连接的fatal错误,copy需谨慎

  • 2017-07-14 13:53:02

    nodejs解决mysql和连接池(pool)自动断开问题

    最近在做一个个人项目,数据库尝试使用了mongodb、sqlite和mysql。分享一下关于mysql的连接池用法。项目部署于appfog,项目中我使用连接池链接数据库,本地测试一切正常。上线以后,经过几次请求两个数据接口总是报503。一直不明就里,今天经过一番排查终于顺利解决了。

  • 2017-07-15 16:13:26

    设置MySQL里的wait_timeout

    如果你没有修改过MySQL的配置,缺省情况下,wait_timeout的初始值是28800。

  • 2017-07-16 20:13:14

    nodejs,express 自制错误日志

    对于同步执行的代码,以上的处理已经足够简单。然而,当异步程序在执行时抛出异常的情况,Express 就无能为力。原因在于当你的程序开始执行回调函数时,它原来的栈信息已经丢失。

  • 2017-07-16 20:17:56

    NodeJS处理Express中异步错误

    本文主要阐述如何在 Express 中使用错误处理中间件(error-handling middleware)来高效处理异步错误。在 Github 上有对应 代码实例 可供参考。