JDK-6559175 : Unchecked cast, List vs List
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic
  • CPU: generic
  • Submitted: 2007-05-18
  • Updated: 2010-04-02
  • Resolved: 2009-03-13
Related Reports
Relates :  
Description
Further checking seems to imply that the answer is NO.
Since the following program is still rejected. So the cast issue a know bug
? Or is the spec being modified to reflect this behavior ?

import java.util.List;
import java.util.Map;

public class X {
      public static void main(String[] args) {
            Object o2 = new List<? extends Object>[3];
      }
}
Playing with unchecked cast semantics, I noticed a strange behavior in
javac (latest 1.5 and 1.6).
I was expecting [2] to issue an unchecked cast warning, according to JLS
5.5; since "List<? extends Object>" doesn't meet the criteria that:
"All of the type arguments (��4.5.1) of T are unbounded wildcards."

Did the spec change to consider "List<?>" the same as "List<? extends
Object>" ? i.e. unbounded or bounded by a no-op bound (same as formal
bound).
I would agree they are essentially the same, but I am curious about the
official saying here.

import java.util.List;

public class X {
  public static void main(String[] args)  {
    Object object = null;
    foo((List<? extends String>)object); //[1]
    foo((List<? extends Object>)object); //[2]
    foo((List<?>)object); //[3]
  }
  public static void foo(List<?> list) {
  }
}

Comments
EVALUATION This got fixed as a side-effect of 6467183 since now the routine for generating unchecked warnings relies on Types.isUnbounded which - in turn - relies on type containment (basically tests as to whether a given type T contains the corresponding unbounded type argument U - in this case yes as ? extends Object contains ?). This allows to remove some trivial and unnecessary warnings. However this doesn't mean that programmers should be allowed to create new instance of a generic class with wildcard parameters as suggested in this CR.
13-03-2009