I have this class, in src/main/java/RawTypes.java
:
import java.util.*;
public class RawTypes {
private List raw;
public void sortRaw() {
raw.addAll(raw);
}
}
If I run javac -Xlint src/main/java/RawTypes.java
, I get 2 warnings: a rawtypes
warning and an unchecked
warning:
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
Which is what I expect.
Here is my 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>
Now, when I run mvn clean compile
, I only get the unchecked
warning:
[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] ------------------------------------------------------------------------
But I don't get the rawtypes
warning.
I'm running OS X, and this happens on both Java 1.7.0_79 and 1.8.0_45. I've tried various versions of the maven-compiler-plugin
with various combinations of specifying the parameter (<compilerArgument>
, <compilerArgs>
, -Xlint
, -Xlint:all
, -Xlint:rawtypes
), but none of them worked.
What's going on? What should I do to get the rawtypes
warning after all?
You need to explicitely set showWarnings
to true
to have the first warning emitted. This is because Maven add by default the -nowarn
option to javac
, which disables the first warning.
If you test with
javac -Xlint -nowarn src/main/java/RawTypes.java
you'll see that the first warning is not emitted anymore.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With