Annotation之四:java -Xlint:unchecked和 -Xlint:deprecation(SuppressWarning抑制警告)

Java Lint 选项 

Java 编译器的选项包括所谓的标准选项和非标准选项。

标准选项是指在当前版本的开发环境中支持,且在未来版本中也将被支持的选项。常用的标准选项比如 -classpath 以及 -d 等等。 

非标准选项是在当前版本的开发环境中支持,但不能保证将来一定会继续支持的选项。非标准选项都由 -X 开头,比如我们这里所关心的 Java Lint 选项都由 -Xlint 开头。需要先说明的是,单纯的选项 -X 本身是一个标准选项,作用是显示关于非标准选项的信息。

目前这些选项都是非标准选项

 javac -X
-Xlint Enable recommended warnings //启用建议的警告 
-Xlint:{all,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs,-cast,-classfile,-deprecation,-dep-ann,
-divzero,-empty,-fallthrough,-finally,-options,-overrides,-path,-processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs,none} Enable or disable specific warnings //启用或禁用特定的警告 -Xbootclasspath/p:<path> Prepend to the bootstrap class path, //置于引导类路径之前 -Xbootclasspath/a:<path> Append to the bootstrap class path // 置于引导类路径之后 -Xbootclasspath:<path> Override location of bootstrap class files //覆盖引导类文件的位置 -Djava.ext.dirs=<dirs> Override location of installed extensions //覆盖安装的扩展目录的位置 -Djava.endorsed.dirs=<dirs> Override location of endorsed standards path //覆盖签名的标准路径的位置 -Xmaxerrs <number> Set the maximum number of errors to print //设置要输出的错误的最大数目 -Xmaxwarns <number> Set the maximum number of warnings to print //设置要输出的警告的最大数目 -Xstdout <filename> Redirect standard output,//重定向标准输出 -Xprint Print out a textual representation of specified types -XprintRounds Print information about rounds of annotation processing -XprintProcessorInfo Print information about which annotations a processor is asked to process -Xprefer:{source,newer} Specify which file to read when both a source file and class file are found for an implicitly compiled class -Xpkginfo:{always,legacy,nonempty} Specify handling of package-info files These options are non-standard and subject to change without notice.

我们依次举几个例子来看看其中涉及 lint 的选项: 

 -Xlint 

启用所有警告。该选项相当于 -Xlint:all 选项。相反,关闭所有警告的选项为 -Xlint:none。 
-Xlint:unchecked 

启用对所谓的未经检查的转换(unchecked warning)的警告。这个警告涉及 JDK 5.0 中的新特性——范型(Generic 
Type),这绝对是另外一个故事了。有兴趣的话,可以在 Internet 上搜索到很多关于范型的文章。 
-Xlint:path 

当发现不存在的路径或者目录时给出警告,比如标准选项中的类路径(classpath),源代码路径(sourcepath)等等。 
-Xlint:serial 

当在可序列化的类中没有发现 serialVersionUID 的定义时,给出警告。 
Xlint:finally 

当发现 finally 子句无法正常结束的情况时给出警告。比如下面这段代码: 

try {  

} catch (Exception e) { 
} finally { 
    for (;;); 

-Xlint:fallthrough 

当在 switch 语句块中发现 fall-through 的情形时给出警告。这里所说的 fall-through 情形指的是在switch语句块中,除了最后一个case 之外的其它 case 中没有包含 break 语句,导致执行代码时从一个 case 直接穿过执行到了下一个 case 中。比如下面这段代码: 

switch (color) { 
case GREEN: 
        System.out.println("green"); 
        // 没有 break 语句;fall-through 
case RED: 
        System.out.println("red"); 
        break; 
case YELLOW: 
        System.out.println("yellow"); 

-Xlint:deprecation 

显示关于使用了过时的 API 的详细信息。这个选项等同于一个我们熟悉的标准选项,即 -deprecation 选项。相对其它 lint 选项而言,该选项的特别之处在于,即使你关闭了它,编译器仍然会简单地提醒你某个类中使用了过时的 API。只是启用该项警告时,显示的信息将更详细而以。 

上面提到的这些 lint 选项中的警告名称(冒号后面的部分),可以加以组合,从而有选择的开启或者关闭特定的一组 lint 警告选项,比如 -Xlint:deprecation, finally 表示开启 deprecation 和 finally 警告。还可以在警告名称前加上"-"表示关闭该项警告,比如 -Xlint:all,-unchecked 表示开启除了 unchecked 之外的所有警告。 

一、-Xlint:unchecked用法

对如下Test.java编译时

package com.dxz.annotation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        ArrayList arr = new Test().getRandom(50);
        Iterator it = arr.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    public ArrayList getRandom(int num) {
        ArrayList randomNum = new ArrayList();
        Random random = new Random();
        int temp = 0;
        for (int i = 0; i < num; i++) {
            temp = random.nextInt(num);
            int nums = temp + 1;
            if (randomNum.indexOf(new Integer(nums)) == -1) {
                randomNum.add(new Integer(nums));
            } else {
                i--;
            }
        }
        return randomNum;
    }
}

用javac编译

javac com/dxz/annotation/Test.java

结果:

增加参数-Xlint:unchecked

javac -Xlint:unchecked com/dxz/annotation/Test.java

结果:

 

 二、-Xlint:deprecation

 参考:https://www.cnblogs.com/timssd/p/5517006.html

posted on 2014-02-19 13:03  duanxz  阅读(3670)  评论(0编辑  收藏  举报