val obj = new Object(); // doesn't matter, any type works
var type = obj.getClass();
var super_type = type.getSuperclass(); // this causes a compile error, however IntelliJ isn't bothered
Main.java:8: error: incompatible types: Class<CAP#1> cannot be converted to Class<? super Object>
var super_type = type.getSuperclass(); // this causes a compile error, however IntelliJ isn't bothered
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends Object super: CAP#2 from capture of ? super CAP#2
CAP#2 extends Object from capture of ?
Expected behavior
var super_type
will be inferred to at the very least Class<?>
.
Version info (please complete the following information):
Lombok version; 1.18.10
Platform: javac 1.8.0_222
The problem here is that the signature of getSuperclass()
of Class<T>
returns Class<? super T>
. Which makes sense. The type of the superclass of T
has an upper bound of T
.
However, if I have a variable of a certain type T
, the value can be any subclass of T
. So the type of getClass()
is ? extends T
. Now the lower bound is T
.
We would need Class<? super (? extends T)>
. That doesn't exist. The only thing that we could generate would be Class<?>
.
For the resolution we depend on javac to give us the correct type. We're not sure if at that point in time we have the right information to fix this problem. We cannot replace all ? super X
results, we should only do that it X
is itself a wildcard (?
or ? extends X
, ? super X
would be fine).