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

Java not implementing generics correctly

XMLWordPrintable

    • 8
    • generic
    • generic

      FULL PRODUCT VERSION :
      C:\Program Files\Java\jdk1.8.0_101\bin>java -version
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 64bit Ultimate Edition

      A DESCRIPTION OF THE PROBLEM :
      class Fruit implements Comparable<Fruit>
      {
          @Override
          public int compareTo(Fruit o) { return 0; }
      }

      // Compiler not giving error upon wrong type passing in generic function

      class Apple extends Fruit{}

      class Main
      {

          public static void main (String args[])
          {
              function(new Apple()); // this shouldn't work because Apple is implementing Comparable<Fruit>, not Comparable<Apple>
          }

          public static<T extends Comparable<T>> void function(T t){}
      }

      REGRESSION. Last worked in version 6u45

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.6.0_45"
      Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
      Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Step 1 - Create Class Fruit

      class Fruit implements Comparable<Fruit>
      {
          @Override
          public int compareTo(Fruit o) { return 0; }
      }

      Step 2 - Create Class Apple

      class Apple extends Fruit
      {
      }

      Step 3 - Create Class Main

      class Main
      {

          public static void main (String args[])
          {
              function(new Apple()); // this shouldn't work because Apple is implementing Comparable<Fruit>, not Comparable<Apple>
          }

          public static<T extends Comparable<T>> void function(T t){}
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      <T extends Comparable<T>> working like <T extends Comparable<? super T>>

      We were expecting compilation error caused by generics

      like compiler gives error upon changing function defination to
              public static<T extends Comparable<T>> void function(List<T> t){}

      and upon calling
              function(new ArrayList<Apple>());


      ACTUAL -
      Compiles without any issue.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class SuperT {

      public static void main (String args[]){

      function(new ArrayList<Apple>()); // generic compilation error
      function(new Apple()); // compiles without issue , although it should give error
      }

      private static<T extends Comparable<T>> void function(T t){

      }

      private static<T extends Comparable<T>> void function(List<T> t){ }
      }

      class Fruit implements Comparable<Fruit>
      {
      @Override
      public int compareTo(Fruit o) { return 0; }
      }

      class Apple extends Fruit{}
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Not yet.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: