The Java Generics FAQ and therefore also cletus' answer sound like there is no point in having Class<List<T>>
, however the real problem is that this is extremely dangerous:
@SuppressWarnings("unchecked")
Class<List<String>> stringListClass = (Class<List<String>>) (Class<?>) List.class;
List<Integer> intList = new ArrayList<>();
intList.add(1);
List<String> stringList = stringListClass.cast(intList);
// Surprise!
String firstElement = stringList.get(0);
The cast()
makes it look as if it is safe, but in reality it is not safe at all.
Though I don't get where there can't be List<?>.class
= Class<List<?>>
since this would be pretty helpful when you have a method which determines the type based on the generic type of a Class
argument.
For getClass()
there is JDK-6184881 requesting to switch to using wildcards, however it does not look like this change will be performed (very soon) since it is not compatible with previous code (see this comment).