JDK-8016636 : cast warning in spite of suppresswarnings when reordering
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: os_x
  • Submitted: 2013-03-20
  • Updated: 2014-11-17
  • Resolved: 2013-06-24
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
javac -version
javac 1.7.0_07


ADDITIONAL OS VERSION INFORMATION :
OS-independent

EXTRA RELEVANT SYSTEM CONFIGURATION :
not configuration-specific

A DESCRIPTION OF THE PROBLEM :
(See also discussion here: http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation)

Code alternative(1) compiles without warnings as it should, but code alternative(2) produces an  " unchecked cast "  warning.

Common for both:

class Foo<T> {
    Foo( T [] arg ) {
    }
}

Alternative (1):

class Bar<T> extends Foo<T> {
    protected static final Object [] EMPTY_ARRAY = {};

    @SuppressWarnings( " unchecked " )
    Bar() {
         super( (T []) EMPTY_ARRAY );
    }
}

Alternative (2):

class Bar<T> extends Foo<T> {
    @SuppressWarnings( " unchecked " )
    Bar() {
         super( (T []) EMPTY_ARRAY );
    }

    protected static final Object [] EMPTY_ARRAY = {};
}

Alternative (2) produces:

javac -Xlint:unchecked Foo.java Bar.java
Bar.java:4: warning: [unchecked] unchecked cast
             super( (T []) EMPTY_ARRAY );
                           ^
  required: T[]
  found:    Object[]
  where T is a type-variable:
    T extends Object declared in class Bar
1 warning

REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile above code samples.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect the @SuppressWarnings to be honored. I also expect that the compiler output does not depend on whether I declare a static variable first or last.

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Reorder declarations.