JDK-8022144 : suppressed unchecked cast still reported in a warning
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u51,7u21,8
  • Priority: P3
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_7
  • Submitted: 2013-05-20
  • Updated: 2013-08-22
  • Resolved: 2013-08-21
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 b98Resolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_21 " 
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
* Linux 3.8.0-21-generic #32-Ubuntu SMP Tue May 14 22:16:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

* Windows 7 x86_64 ('ver' N/A at the moment)

A DESCRIPTION OF THE PROBLEM :
Under specific circumstances, @SuppressWarnings( " unchecked " ) doesn't work. The example follows (two classes, A.java and B.java, to be put in one directory), including the detailed description and a guess of the general pattern when the bug occurs:

--- A.java ---

import java.util.List;

public class A {

    // The first one of either this:
    @SuppressWarnings( " unchecked " )
    List<String> a = (List) (Object) B.STRING;

    static void a() {
        // or this:
        @SuppressWarnings( " unchecked " )
        List<String> a = (List) (Object) B.STRING;
    }

    static void b() {
        // or this:
        @SuppressWarnings( " unchecked " )
        List<String> a = (List) f(B.STRING, null);

        // or this:
        @SuppressWarnings( " unchecked " )
        List<String> b = (List) f(null, B.STRING);
    }

    static Object f(Object o, String s) {
        return null;
    }

    // etc. causes

    //     A.java:x: warning: [unchecked] unchecked conversion
    //       List<String> x = (List) ...
    //                        ^
    //       required: List<String>
    //       found:    List
    //     1 warning

    // when 'touch B.java && javac A.java -Xlint:unchecked' is run.
    //
    // Remarks:
    //   * exhibited by javac 1.7.0_21 (Linux/Windows x64); doesn't occur for javac 1.6.0_27 (Linux/Windows x64)
    //
    //   * if B.STRING is not declared 'final', or if it is referred to in any way in A prior to any of the unchecked
    //     casts above, like in

    //         public class A {
    //             String aux = B.STRING;
    //             ...

    //     the bug is not triggered.
    //
    // Summary: it seems that the bug appears for every suppressed unchecked cast containing the first occurrence
    // (within the current class) of a reference to a 'final' (*) field of another class, where the latter class is
    // deemed necessary to be (re)compiled by javac (in particular, when it hasn't been compiled yet).
    //
    // (*) 'static' modifier is not necessary for triggering the bug: even though without it the compilation fails with
    // error(s), the unchecked-cast warning is still issued.
}

--- B.java ---

public class B {
    public static final String STRING = null;
}


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. put A.java and B.java in one directory, copying their contents from 'Description' form above

2. run 'touch B.java && javac A.java -Xlint:unchecked'

3. for further details, refer to comments in A.java above

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
no warning issued
ACTUAL -
A.java:7: warning: [unchecked] unchecked conversion
    List<String> a = (List) (Object) B.STRING;
                     ^
  required: List<String>
  found:    List
1 warning

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
--- A.java ---

import java.util.List;

public class A {

    // The first one of either this:
    @SuppressWarnings( " unchecked " )
    List<String> a = (List) (Object) B.STRING;

    static void a() {
        // or this:
        @SuppressWarnings( " unchecked " )
        List<String> a = (List) (Object) B.STRING;
    }

    static void b() {
        // or this:
        @SuppressWarnings( " unchecked " )
        List<String> a = (List) f(B.STRING, null);

        // or this:
        @SuppressWarnings( " unchecked " )
        List<String> b = (List) f(null, B.STRING);
    }

    static Object f(Object o, String s) {
        return null;
    }

    // etc. causes

    //     A.java:x: warning: [unchecked] unchecked conversion
    //       List<String> x = (List) ...
    //                        ^
    //       required: List<String>
    //       found:    List
    //     1 warning

    // when 'touch B.java && javac A.java -Xlint:unchecked' is run.
    //
    // Remarks:
    //   * exhibited by javac 1.7.0_21 (Linux/Windows x64); doesn't occur for javac 1.6.0_27 (Linux/Windows x64)
    //
    //   * if B.STRING is not declared 'final', or if it is referred to in any way in A prior to any of the unchecked
    //     casts above, like in

    //         public class A {
    //             String aux = B.STRING;
    //             ...

    //     the bug is not triggered.
    //
    // Summary: it seems that the bug appears for every suppressed unchecked cast containing the first occurrence
    // (within the current class) of a reference to a 'final' (*) field of another class, where the latter class is
    // deemed necessary to be (re)compiled by javac (in particular, when it hasn't been compiled yet).
    //
    // (*) 'static' modifier is not necessary for triggering the bug: even though without it the compilation fails with
    // error(s), the unchecked-cast warning is still issued.
}

--- B.java ---

public class B {
    public static final String STRING = null;
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
include any reference to the offending 'final' field prior to the problematic unchecked cast
Comments
I believe this has been fixed together with/by the fix of bug JDK-8016099.
21-08-2013

SQE is ok to defer from 7u40.
05-08-2013

This is a valid bug and can be reproduced on jdk8-b94, this is an old issue seems to be prevalent from 6uX
02-08-2013