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

Wrong version of overloaded method is selected by compiler

    XMLWordPrintable

Details

    • generic
    • generic
    • Not verified

    Description

      ADDITIONAL SYSTEM INFORMATION :
      CentOS Linux release 7.9.2009 (Core)

      OpenJDK Runtime Environment (build 1.8.0_322-b06)
      OpenJDK Server VM (build 25.322-b06, mixed mode)
      as well as
      Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      I am able to reproduce the following old bug on the latest Java 8 releases (both Oracle and OpenJDK):
      https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8024495

      The bug does not occur on MS Windows platform, only on CentOS Linux.







      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached source code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "Create object"
      ACTUAL -
      Exception in thread "main" java.lang.ClassCastException: Test$1 cannot be cast to java.util.List
              at Test.test(Test.java:14)
              at Test.main(Test.java:10)

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

      public class Test {
          public interface MyIface {

          }

          public static void main(String[] args) {
              Test t = new Test();
              t.test();
          }

          public void test() {
              create(loadById("foo"));
          }

          private <E extends MyIface> E loadById(String str) {
              return (E) new MyIface() {
              };
          }

          public void create(Object o) {
              System.out.println("Create object");
          }

          public void create(List<?> list) {
              System.out.println("Create list");
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Adding explicit cast will solve the problem:
      create((Object)loadById("foo"));

      FREQUENCY : always


      Attachments

        Activity

          People

            adev ANUPAM DEV
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: