JDK-4922142 : generic enclosing type in cast: allowed?
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_8
  • CPU: generic
  • Submitted: 2003-09-12
  • Updated: 2014-02-26
  • Resolved: 2011-07-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 7
7 rcFixed
Related Reports
Relates :  
Description
Date: Sat, 06 Sep 2003 01:13:26 +0100
From: Russel Winder <###@###.###>
Subject: Incongruous or just a misunderstanding?
To: Neal M Gafter <###@###.###>
Cc: Graham Roberts <###@###.###>

I have a generic type DLList which is a double linked list.  As an inner
class there is Iterator.  I find that the compile lets me use
DLList<Pair<K, V>>.Iterator as a type name in a declaration but not as a
type name in a cast.  So

DLList<Pair<K, V>>.Iterator i = (DLList<Pair<K, V>>.Iterator)X.iterator();

fails because the cast syntax is not acceptable but then that means
that:

DLList<Pair<K, V>>.Iterator i = (DLList.Iterator)X.iterator() ;

fails because there is an unchecked assignment.  A priori I would have
thought the same type names could be used in both places.

Comments
EVALUATION Interestingly, the compiler treats the cast operator's TypeName as a parameterized type here: class Foo { class Bar<T> { } } Foo f = new Foo(); Foo.Bar<String> g = (Foo.Bar<String>)f.new Bar<String>(); // Cast OK but not here: class Foo { class Bar<T> { class Cha {} } } Foo f = new Foo(); Foo.Bar<String> g = f.new Bar<String>(); Foo.Bar<String>.Cha h = (Foo.Bar<String>.Cha) g.new Cha(); // Cast not OK As per the previous evaluation, we could modify JLS4.5 to include inner classes, i.e. from: "A parameterized type consists of a class or interface name C and an actual type argument list <T1,...,Tn>." to: "A parameterized type is a type that includes a class or interface name C followed by an actual type argument list <T1,...,Tn>." Then, the Foo.Bar<...>.Cha cast is legal (and happens to be completely unchecked).
14-11-2006

EVALUATION The spec isn't clear on this. Specifically, it isn't clear whether DLL<Bla, Bla>.Foo is a parameterized type, or reifiable. Of course the answers are yes and no respectively, but the text needs to make that clear. ###@###.### 2004-06-03
03-06-2004