我有这门课,在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
,我收到 2 个警告:一个 rawtypes
警告和 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>
关于java - -Xlint :rawtypes not working in Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247099/