JDK-6181716 : int.class has type Class
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2004-10-20
  • Updated: 2010-04-02
  • Resolved: 2005-05-19
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 6
6Resolved
Related Reports
Relates :  
Relates :  
Description
The type of int.class is Class<Integer>.

This basically destroys the type safety purported by type tokens:

import java.util.*;

class Test {
    public static void main(String[] args) {
        Collection<Integer> c = new Vector<Integer>();
        c = Collections.checkedCollection(c, int.class);
        c.add(0);
    }
}

This program fails:
Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer element into collection with element type int
        at java.util.Collections$CheckedCollection.typeCheck(Collections.java:2206)
        at java.util.Collections$CheckedCollection.add(Collections.java:2240)
        at Test.main(Test.java:7)
###@###.### 10/20/04 06:31 GMT

Comments
EVALUATION I hope this is a bug in the compiler and not the specification. I'm currently wating for feedback on that. ###@###.### 10/20/04 06:31 GMT Behavior is as specified. ###@###.### 2005-05-19 01:55:47 GMT
20-10-2004

SUGGESTED FIX Index: src/share/classes/com/sun/tools/javac/comp/Attr.java =========================================================== @@ -1821,7 +1821,9 @@ // In this case, we have already made sure in Select that // qualifier expression is a type. Type t = syms.classType; - Type arg = types.boxedClass(site).type; + Type arg = new ArgumentType(syms.objectType, + BoundKind.UNBOUND, + syms.boundClass); t = new ClassType(t.outer(), Type.emptyList.prepend(arg), t.tsym); return new VarSymbol( STATIC | PUBLIC | FINAL, names._class, t, site.tsym); ###@###.### 10/20/04 06:29 GMT
20-10-2004