-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
8, 11, 14, 16
-
x86_64
-
windows_10
A DESCRIPTION OF THE PROBLEM :
When a wildcard is used for a type variable with an upper bound, javac (and possibly the JLS as well) permits using any supertype of the bound of the type variable as upper bound of the wildcard. This allows writing highly confusing code (by accident), see the code example below.
Side note: For lower bounds of wildcards this is not possible. They are restricted to the same or subtypes of the type variable bound.
---------- BEGIN SOURCE ----------
class WildcardBoundsTest {
static class BaseClass {}
static class SubClass extends BaseClass {}
static class GenericClass<T extends SubClass> {}
// Bound of wildcard is supertype of type variable bound
// This is illogical because only SubClass or subtypes are permitted
// as type argument; BaseClass or any other subtype are not
void doSomething(GenericClass<? extends BaseClass> param) {}
// Side note: This is not permitted by javac
// void doSomething2(GenericClass<? super BaseClass> param) {}
}
---------- END SOURCE ----------
When a wildcard is used for a type variable with an upper bound, javac (and possibly the JLS as well) permits using any supertype of the bound of the type variable as upper bound of the wildcard. This allows writing highly confusing code (by accident), see the code example below.
Side note: For lower bounds of wildcards this is not possible. They are restricted to the same or subtypes of the type variable bound.
---------- BEGIN SOURCE ----------
class WildcardBoundsTest {
static class BaseClass {}
static class SubClass extends BaseClass {}
static class GenericClass<T extends SubClass> {}
// Bound of wildcard is supertype of type variable bound
// This is illogical because only SubClass or subtypes are permitted
// as type argument; BaseClass or any other subtype are not
void doSomething(GenericClass<? extends BaseClass> param) {}
// Side note: This is not permitted by javac
// void doSomething2(GenericClass<? super BaseClass> param) {}
}
---------- END SOURCE ----------