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

Compiler error on generic intersection type declaration with two similar interfaces, one with a default implementation

XMLWordPrintable

    • generic
    • generic

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14393]

      A DESCRIPTION OF THE PROBLEM :
      Java compiler "javac" fails on the generic intersection type declaration '<T extends I1 & IDefault>' where I1 and IDefault are interfaces, both defining the same method ('String get();'), one with a default implementation ('default String get() {return "default";};').

      See also https://stackoverflow.com/q/45798233/6505250

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile following code via "javac Foo.java":

      interface I1 {
          String get();
      }

      interface I2 {
          String get();
      }

      interface IDefault {
          default String get() {
           return "default";
          };
      }

      public class Foo implements I1, I2, IDefault {

          @Override
          public String get() {
              return "foo";
          }

          public static void main(String[] args) {
              System.out.print(getOf(new Foo()));
          }

       // static <T extends I1 & I2> String getOf(T t) { // OK
       // static <T extends I1, IDefault> String getOf(T t) { // OK
          static <T extends I1 & IDefault> String getOf(T t) { // OK with Eclipse compiler, but fails with javac:
       // ^
       // where INT#1 is an intersection type:
       // INT#1 extends Object,I1,IDefault
       //1 error

              return t.get();
          }

      }



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No compiler error (as with the Eclipse compiler)
      ACTUAL -
      Compiler error (see error message below)


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
              static <T extends I1 & IDefault> String getOf(T t) {
                      ^
        where INT#1 is an intersection type:
          INT#1 extends Object,I1,IDefault
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      interface I1 {
          String get();
      }

      interface I2 {
          String get();
      }

      interface IDefault {
          default String get() {
           return "default";
          };
      }

      public class Foo implements I1, I2, IDefault {

          @Override
          public String get() {
              return "foo";
          }

          public static void main(String[] args) {
              System.out.print(getOf(new Foo()));
          }

       // static <T extends I1 & I2> String getOf(T t) { // OK
       // static <T extends I1, IDefault> String getOf(T t) { // OK
          static <T extends I1 & IDefault> String getOf(T t) { // OK with Eclipse compiler, but fails with javac:
       // ^
       // where INT#1 is an intersection type:
       // INT#1 extends Object,I1,IDefault
       //1 error

              return t.get();
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use Eclipse compiler (Eclipse Oxygen/4.7) or avoid such code

        1. Foo.java
          0.8 kB
          Fairoz Matte

            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: