JDK-6229784 : (reflect) Overlooked generification of java.lang.Class
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,linux
  • CPU: generic,x86
  • Submitted: 2005-02-16
  • Updated: 2017-05-16
  • Resolved: 2005-12-03
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
6 b63Fixed
Related Reports
Duplicate :  
Relates :  
Description
###@###.### writes:

Can you tell me why the methods in java.lang.Class that return arrays,
always return raw types instead of parameterized types?
 
eg. why does getInterfaces return Class[] instead of Class<?>[]

    why does getClasses return Class[] instead of Class<?>[]

    why does getConstructors return Constructor[] instead of Constructor<T>[]

###@###.### writes:

For the first two, this is a holdover from a time when you weren't
allowed to write arrays of generic types at all.  In this case it
would have been safe to use Class<?>[].  I think we just overlooked
generifying it in this case.  It should have been as you suggest.

For Constructor<T>, on the other hand, there is actually a type hole
that would be introduced by typing it that way:

  Constructor<Foo>[] cfoo = Foo.class.getConstructors();
  Object[] a = cfoo;
  a[0] = Bar.class.getConstructor();
  Foo foo = cfoo[0].newInstance(); // BOOM!

###@###.### 2005-2-16 07:04:22 GMT

Comments
SUGGESTED FIX src/share/classes/java/lang>sccs sccsdiff -r1.192 -r1.193 Class.java ------- Class.java ------- 741c741 < public native Class[] getInterfaces(); --- > public native Class<?>[] getInterfaces(); 1275c1275 < public Class[] getClasses() { --- > public Class<?>[] getClasses() { 1419a1420,1429 > * Note that while this method returns an array of {@code > * Constructor<T>} objects (that is an array of constructors from > * this class), the return type of this method is {@code > * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as > * might be expected. This less informative return type is > * necessary since after being returned from this method, the > * array could be modified to hold {@code Constructor} objects for > * different classes, which would violate the type guarantees of > * {@code Constructor<T>[]}. > * 1443c1453 < public Constructor[] getConstructors() throws SecurityException { --- > public Constructor<?>[] getConstructors() throws SecurityException { src/share/classes/com/sun/xml/bind/v2/runtime/property>sccs sccsdiff PropertyFactory.java | morsccs sccsdiff -r1.4 -r1.5 PropertyFactory.java ------- PropertyFactory.java ------- 53c53 < propImpls[i] = implClasses[i].getConstructors()[0]; --- > propImpls[i] = (Constructor)implClasses[i].getConstructors()[0];
29-11-2005

EVALUATION Arrays of raw types should not be returned. In the case of getConstructors, unfortunately to maintain type safety Constructor<?>[] needs to be returned instead of Constructor<T>[] .
08-11-2005