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

(reflect) Method.getReturnType() returns wrong type for overriding methods (generics)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • tools
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      Consider the following examples classes:

      public class Example1<E extends Collection> {
        public E process(){...}
      }
       
      public class Example2 extends Example1<List> {}

      Calling Example2.getMethod("process").getReturnType() returns 'java.util.Collection' and not, as expected 'java.util.List'.

      As class Example2 itself is not generic I consider this inaccurate.

      Note: I already reported this bug against JDK 1.5.0 but never got an answer.

      The following (legal) code examples illustrates that the return type of Example2.process() is obviously 'List' and not 'Collection':

      public class Example3 {
        public void test() {
          Example2 example2 = new Example2();
          List test = example2.process();
        }
      }

      This problem was already discussed in http://forum.java.sun.com/thread.jspa?threadID=707880

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See description or use example code below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect Example2.getMethod("process").getReturnType() to return 'java.util.List' (see description).
      ACTUAL -
      Actual result is 'java.util.Collection' (the lower bound of generic parameter E in class 'Example1'. If the class does not have a lower bound, 'java.lang.Object' is returned.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Collection;
      import java.util.List;

      public class ReflectionBugDemo {
        public static void main(String[] args) throws SecurityException, NoSuchMethodException {
          Class<?> returnType1 = Example1.class.getMethod("process").getReturnType();
          Class<?> returnType2 = Example2.class.getMethod("process").getReturnType();
          
          System.out.println("Return type of Example1.process(): " +returnType1);
          System.out.println("Return type of Example2.process(): " +returnType2);
        }
        
      }

      class Example1<E extends Collection> {
        public E process() {
          return null;
        }
      }

      class Example2 extends Example1<List> {}
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There was a workround presented in http://forum.java.sun.com/thread.jspa?threadID=707880 but these (and other solution I found myself) are extremely cumbersome.

            ahe Peter Ahe
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: