-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
8u391, 11.0.21, 17.0.9, 21.0.1
-
generic
-
generic
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
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