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

Reference to [method] is ambiguous

XMLWordPrintable

    • x86
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      The following class cannot be compiled with JDK 8, but can with JDK 7 and below.

      The "get" method has a generic return type; however, the compiler indicates that both the String and Integer overloaded "put" methods are valid, completely ignoring the Object method which should be chosen.

      If either the String or Integer methods are removed, the compiler chooses wisely. If another overloaded method is added, the compiler will then indicate the "new" last two methods are ambiguous.

      Its a minor inconvenience to be sure as the output from the get method can be explicitly cast to an Object.

      public class X {
          
          public static <R> R get(String d) {
              return (R)d;
          }
          
          public static void main(String[] args) throws Exception {
              put(get("hello"));
          }
          
          public static void put(Object o) {
          }
          
          public static void put(String c) {
          }
          
          public static void put(Integer c) {
          }
           
      }

      REGRESSION. Last worked in version 7u79

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_79"
      Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
      Java HotSpot(TM) Client VM (build 24.79-b02, mixed mode, sharing)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile the X class with JDK 1.8.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compiler should choose the correct overloaded method based on the output of the generic-returning method.
      ACTUAL -
      error: reference to put is ambiguous
              put(get("hello"));
        both method put(String) in X and method put(Integer) in X match

      REPRODUCIBILITY :
      This bug can be reproduced always.

            sadayapalam Srikanth Adayapalam (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: