Assume the following methods (Java 8):
public <T> List<Class<T>> getList() {
return new ArrayList<>();
}
public <T> List<T> getList2() {
return new ArrayList<>();
}
And the following code using those methods:
@Test
public void testLists() {
getList().add(String.class); // Does not compile
this.<String>getList().add(String.class); // Compiles
getList2().add(String.class); // Compiles
}
The second and third call compile fine, whereas the first one gives:
add(java.lang.Class<java.lang.Object>)
in List cannot be applied toadd(java.lang.Class<java.lang.String>)
I do not fully understand the resolution steps here, maybe someone could explain why I need the type witness and the compiler does not infer the type automatically?