我在src/main/java/RawTypes.java
:
import java.util.*;
public class RawTypes {
private List raw;
public void sortRaw() {
raw.addAll(raw);
}
}
如果我运行javac -Xlint src/main/java/RawTypes.java
,rawtypes
则会收到2条警告:警告和unchecked
警告:
src/main/java/RawTypes.java:4: warning: [rawtypes] found raw type: List
private List raw;
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
src/main/java/RawTypes.java:7: warning: [unchecked] unchecked call to addAll(Collection<? extends E>) as a member of the raw type List
raw.addAll(raw);
^
where E is a type-variable:
E extends Object declared in interface List
2 warnings
这是我所期望的。
这是我的pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.jqno.rawtypes</groupId>
<artifactId>rawtypes</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<encoding>UTF-8</encoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<compilerArgument>-Xlint</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
现在,当我运行时mvn clean compile
,我只会得到unchecked
警告:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building rawtypes 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ rawtypes ---
[INFO] Deleting /Users/jqno/javarawtypes/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rawtypes ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jqno/javarawtypes/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5:compile (default-compile) @ rawtypes ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/jqno/javarawtypes/target/classes
[WARNING] /Users/jqno/javarawtypes/src/main/java/RawTypes.java:[7,19] unchecked call to addAll(java.util.Collection<? extends E>) as a member of the raw type java.util.List
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.903 s
[INFO] Finished at: 2016-02-06T23:03:37+01:00
[INFO] Final Memory: 15M/267M
[INFO] ------------------------------------------------------------------------
但是我没有得到rawtypes
警告。
我正在运行OS X,这在Java 1.7.0_79和1.8.0_45上都发生。我已经试过各种版本maven-compiler-plugin
与指定参数的各种组合(<compilerArgument>
,<compilerArgs>
,-Xlint
,-Xlint:all
,-Xlint:rawtypes
),但他们没有工作。
这是怎么回事?到底该怎么做才能得到rawtypes
警告?
您需要明确设置showWarnings
为true
发出第一个警告。这是因为Maven默认将-nowarn
选项添加到javac
,从而禁用了第一个警告。
如果用
javac -Xlint -nowarn src/main/java/RawTypes.java
您会看到不再发出第一个警告。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<compilerArgument>-Xlint</compilerArgument>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句