我在 javac 中使用
-Werror
标志,将所有警告视为错误(与本文相关:使 javac 将警告视为错误)。
但是,我试图从该规则中排除弃用,以便弃用仍然被视为警告而不是错误。 有没有办法用另一个编译器标志来做到这一点?
添加
-Xlint:-deprecation
javac \
-Werror \
-Xlint:all \
-Xlint:-deprecation \
Main.java
示例:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Main {
public static void main(String[] args) {
List rawList = new ArrayList(); //warning: [rawtypes] found raw type: ArrayList
rawList.add("Hello"); // warning: [unchecked] unchecked call to add(E) as a member of the raw type List
List<String> stringList = (List<String>) rawList; // warning: [unchecked] unchecked cast
System.out.println(stringList.get(0)); //Hello
Date date = new Date();
int year = date.getYear(); // warning: [deprecation] getYear() in Date has been deprecated
int month = date.getMonth(); // warning: [deprecation] getMonth() in Date has been deprecated
int day = date.getDay(); // warning: [deprecation] getDay() in Date has been deprecated
System.out.println(year + "-"+ month+"-"+day );
} //main
} //class
-Werror
javac \
-Werror \
-Xlint:all \
Main.java
获取
Main.java:8: warning: [rawtypes] found raw type: List
List rawList = new ArrayList(); //warning: [rawtypes] found raw type: ArrayList
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
Main.java:8: warning: [rawtypes] found raw type: ArrayList
List rawList = new ArrayList(); //warning: [rawtypes] found raw type: ArrayList
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
Main.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
rawList.add("Hello"); // warning: [unchecked] unchecked call to add(E) as a member of the raw type List
^
where E is a type-variable:
E extends Object declared in interface List
Main.java:11: warning: [unchecked] unchecked cast
List<String> stringList = (List<String>) rawList; // warning: [unchecked] unchecked cast
^
required: List<String>
found: List
Main.java:16: warning: [deprecation] getYear() in Date has been deprecated
int year = date.getYear(); // warning: [deprecation] getYear() in Date has been deprecated
^
Main.java:17: warning: [deprecation] getMonth() in Date has been deprecated
int month = date.getMonth(); // warning: [deprecation] getMonth() in Date has been deprecated
^
Main.java:18: warning: [deprecation] getDay() in Date has been deprecated
int day = date.getDay(); // warning: [deprecation] getDay() in Date has been deprecated
^
error: warnings found and -Werror specified
1 error
7 warnings
-Xlint:-deprecation
javac \
-Werror \
-Xlint:all \
-Xlint:-deprecation \
Main.java
获取
Main.java:8: warning: [rawtypes] found raw type: List
List rawList = new ArrayList(); //warning: [rawtypes] found raw type: ArrayList
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
Main.java:8: warning: [rawtypes] found raw type: ArrayList
List rawList = new ArrayList(); //warning: [rawtypes] found raw type: ArrayList
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
Main.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
rawList.add("Hello"); // warning: [unchecked] unchecked call to add(E) as a member of the raw type List
^
where E is a type-variable:
E extends Object declared in interface List
Main.java:11: warning: [unchecked] unchecked cast
List<String> stringList = (List<String>) rawList; // warning: [unchecked] unchecked cast
^
required: List<String>
found: List
error: warnings found and -Werror specified
Note: Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
4 warnings
-Werror
-Werror
,-Xlint:-deprecation
警告忽略
deprecation
:
int year = date.getYear(); // warning: [deprecation] getYear() in Date has been deprecated
int month = date.getMonth(); // warning: [deprecation] getMonth() in Date has been deprecated
int day = date.getDay(); // warning: [deprecation] getDay() in Date has been deprecated