following code fails compiling the last line even though the second last one compiles ok while the difference is only in generic type that seems to be well within its bounds.
can anyone explain? I guess it relates to erasure but why the second-last statement compiles ok?
public class Main {
public static class A {
public class Inner {
}
}
public static class B extends A {
public class Inner {
}
}
public static class Wrap<T extends A> {
public Class<T.Inner> get(Class<T.Inner> cls) {
return cls;
}
}
public static void main(String... args) {
Wrap<A> wa = new Wrap<>();
Wrap<B> wb = new Wrap<>();
// this compiles OK
Class<A.Inner> ai = wa.get(A.Inner.class);
// this fails
Class<B.Inner> bi = wb.get(B.Inner.class);
}
}
compiler error:
/tmp$ javac -Xdiags:verbose Main.java
Main.java:24: error: method get in class Wrap<T> cannot be applied to given types;
Class<B.Inner> bi = wb.get(B.Inner.class);
^
required: Class<Main.B.Inner>
found: Class<Main.B.Inner>
reason: argument mismatch; Class<Main.B.Inner> cannot be converted to Class<Main.B.Inner>
where T is a type-variable:
T extends Main.A declared in class Wrap
1 error