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

Generics case does not compile

XMLWordPrintable

    • x86
    • windows_xp

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

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

      A DESCRIPTION OF THE PROBLEM :
      Compiler fails to infer the correct type of a wildcard capture. Given a class A<T extends A<T>> with a method T get(), the compiler should be able to infer that the return type when calling get() on an instance of A<?> is <T extends A<T>>.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac Test.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Successfully compiles.
      ACTUAL -
      Compile fails with error:

      Test.java:10: <T>f1(T) in Test cannot be applied to (Test.A<capture#282 of ?>)
          f1(obj.get()); // Fails to compile

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
        class A<T extends A<T>> {
          T get() {return null;}
        }
        
        <T extends A<T>> void f1(T t) {}
        <T extends A<?>> void f2(T t) {}
        
        void main(A<?> obj) {
          f1(obj.get()); // Fails to compile
          f2(obj.get()); // Works
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use method f2 instead of f1.

            dlsmith Dan Smith
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: