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

lower bound assignment in List.of() is not throwing error.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u391, 11.0.21, 17.0.9, 21.0.1
    • core-libs

      A DESCRIPTION OF THE PROBLEM :
      Lower bound checks are not happening properly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

      import java.util.List;

      class Animal {
      }

      class Dog extends Animal {
      }

      class Cat extends Animal {
      }

      public class TestLower {

      public static void main(String[] args) {
      System.out.println("start");

      //The below line is throwing error if i pass new Object()/any other object
      //List<? extends Animal> lo= List.of(new Animal(), new Cat(), new Dog(), new Object());

      List<? extends Animal> lo = List.of(new Animal(), new Cat(), new Dog());

      // Upper bound looks fine.
      Animal a = lo.get(0);
      // Throws Error...Good..
      // lo.add(new Dog());

      //Lower bound has some issues, its accepting String,Integet,Object while adding it list.of() method.
      //I think it has to throw error like in upper bound if we add not related objects.
      List<? super Animal> lo2 = List.of(new Animal(), new Cat(), new Dog(), new Object(), new String(),
      new Integer(0));
      // lo2.add(new Dog());//Throws Error...Good..
      // lo2.add(new String());//Throws Error...Good..
      System.out.println("end");

      }

      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      for lower bound generics it should throw error like upper bound.
      ACTUAL -
      lowerbound is accepting any object from List.of method.

      ---------- BEGIN SOURCE ----------

      import java.util.List;

      class Animal {
      }

      class Dog extends Animal {
      }

      class Cat extends Animal {
      }

      public class TestLower {

      public static void main(String[] args) {
      System.out.println("start");

      //The below line is throwing error if i pass new Object()/any other object
      //List<? extends Animal> lo= List.of(new Animal(), new Cat(), new Dog(), new Object());

      List<? extends Animal> lo = List.of(new Animal(), new Cat(), new Dog());

      // Upper bound looks fine.
      Animal a = lo.get(0);
      // Throws Error...Good..
      // lo.add(new Dog());

      //Lower bound has some issues, its accepting String,Integet,Object while adding it list.of() method.
      //I think it has to throw error like in upper bound if we add not related objects.
      List<? super Animal> lo2 = List.of(new Animal(), new Cat(), new Dog(), new Object(), new String(),
      new Integer(0));
      // lo2.add(new Dog());//Throws Error...Good..
      // lo2.add(new String());//Throws Error...Good..
      System.out.println("end");

      }

      }

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

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: