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

Ambiguity with covariant parameterized static methods

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • 8
    • 6
    • tools
    • x86
    • windows_xp

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

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

      A DESCRIPTION OF THE PROBLEM :
      For static methods with type parameters and covariant return types involving these parameters, method hiding doesn't work as expected. (See the provided example.)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the provided test case.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Bar2.newInstance() should hide Foo2.newInstance(), just as Bar2.newInstance() hides Foo2.newInstance().

      ACTUAL -
      javac reports errors on the bar2 assignment.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Test.java:28: reference to newInstance is ambiguous, both method <S,T>newInstance() in Test.Foo2 and method <T>newInstance() in Test.Bar2 match
                  Bar2<Number> bar2 = Bar2.newInstance();
      Test.java:28: incompatible types; no instance(s) of type variable(s) S,T exist so that Test.Foo2 conforms to Test.Bar2<java.lang.Number>
      found : <S,T>Test.Foo2
      required: Test.Bar2<java.lang.Number>
                  Bar2<Number> bar2 = Bar2.newInstance();
      2 errors


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      public class Test
      {
          static class Foo<T>
          {
              static <T> Foo newInstance() { return new Foo<T>(); }
          }

          static class Bar extends Foo<String>
          {
              static Bar newInstance() { return new Bar(); }
          }

          static class Foo2<S, T>
          {
              static <S, T> Foo2 newInstance() { return new Foo2<S, T>(); }
          }

          static class Bar2<T> extends Foo2<String, T>
          {
              static <T> Bar2<T> newInstance() { return new Bar2<T>(); }
          }

          public Test()
          {
              Bar bar = Bar.newInstance(); // ok
              Bar2<Number> bar2 = Bar2.newInstance(); // compile error
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Explicitly specify type arguments in the call, e.g.:

          Bar2<Number> bar2 = Bar2.<Number>newInstance();

            mcimadamore Maurizio Cimadamore
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: