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

Apparently valid Java code with generics compiles with Eclipse but not with javac

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+161)
      Java HotSpot(TM) 64-Bit Server VM (build 9-ea+161, mixed mode)

      (Tried with both)

      ADDITIONAL OS VERSION INFORMATION :
      Linux new-host 4.9.13-201.fc25.x86_64 #1 SMP Tue Mar 7 23:47:11 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      Try to compile the provided program. No matter what you do, the program cannot be compiled. javac complains about "ambiguous method", or if you try to cast the argument of the last call to Object, that no method can be found.

      Eclipse compiles the source providing the expected result (the most specific method is called).


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just compile the source provided.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      > javac Test.java
      Test.java:19: error: reference to method is ambiguous
      c.method(Integer.valueOf(0));
      ^
        both method method(Integer) in B and method method(K) in A match
        where K is a type-variable:
          K extends Object declared in interface A
      1 error


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      interface A<K> {
              void method(K k);
      }

      interface B {
              void method(Integer k);
      }

      interface C extends A<Integer>, B {
      }

      class D implements C {
              public void method(Integer i) {}
      }

      public class Test {
              public static void main(String args[]) {
                      C c = new D();
                      c.method(Integer.valueOf(0));
              }
      }

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

        1. Test.java
          0.4 kB
          Fairoz Matte

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: