Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6229784

(reflect) Overlooked generification of java.lang.Class

XMLWordPrintable

    • b63
    • generic, x86
    • generic, linux

      ###@###.### 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

            darcy Joe Darcy
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: