JDK-8021112 : Spurious unchecked warning reported by javac
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2013-07-23
  • Updated: 2013-10-08
  • Resolved: 2013-09-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b108Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
java.lang.Integer and the other wrapper classes contain code like

    @SuppressWarnings("unchecked")
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

where Class.getPrimitiveClass is a package-private static method returning a Class<?>.

Despite the @SuppressWarnings annotation, javac still reports an unchecked warning on this code:

src/share/classes/java/lang/Integer.java:72: warning: [unchecked] unchecked cast
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
                                                                                       ^
  required: Class<Integer>
  found:    Class<CAP#1>
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?

This behavior is unfortunate since it frustrates efforts to resolve lint warnings in the JDK.
Comments
A standalone testcase: ----- A.java: import java.util.List; public class A { public static final List f = null; @SuppressWarnings("unchecked") public static final List<String> a = (List<String>) f; } ----- ----- B.java: import java.util.List; public class B { public static final List<String> x = A.a; } ---- Compiling first B.java and then A.java shows the problem: ----- $ javac -Xlint:unchecked B.java A.java A.java:7: warning: [unchecked] unchecked cast public static final List<String> a = (List<String>) f; ^ required: List<String> found: List 1 warning ---- Compiling the files in the opposite order does not: ----- $ javac -Xlint:unchecked A.java B.java ----- This is with: $ javac -fullversion javac full version "1.8.0-ea-b103"
22-08-2013

I tried looking at this during the lint warnings cleanup day. I couldn't get it to reproduce outside a jdk build. Here is what I think happens: - The warning is an unchecked conversion warning, probably originating in Check.ConversionWarner (Check.java) - Whenever I trigger something like this in the debugger, the nonSilentLintSet and silentLintSet inherited from Warner contains the correct lint categories Some speculation: - So I would guess that there is a path to creating a ConversionWarner that sometimes does not use the correct lint env - The linked issue suggests there may be some correlation to field initializers. The rhs of the assignment might not have the lint context of the declaration which carries the @SuppressWarnings annotation
06-08-2013