I use the .class
-operator to supply information about the contained type to a generic class. For non-generic contained types, e.g. Integer.class
, this works without any problems. But with the contained type being a generic, e.g. List<Integer>.class
or List.class
it results in compile time errors about class casting.
There is a way to circumvent the errors, but I'm curious about what is happening here. Can someone explain what is happening?, why things are as they are?, and what the best way to circumvent the problem is?
The following lines demonstrate the problem:
Note the outer generic type expects Class<T>
as parameter, so in this case Class<List<Integer>>
.
Class<Integer> tInt = Integer.class; // Works as expected.
Class<List> tList = List.class; // Works with warning, but is not
// what i'm looking for.
Class<List<Integer>> tListInt1 = List.class; // Error
Class<List<Integer>> tListInt2 = (Class<List<Integer>>) List.class; // Error
Class<List<?>> tListGeneric = (Class<List<Integer>>) List.class; // Error
The next line works:
Class<List<Integer>> tListInt3 =
(Class<List<Integer>>) ((Class<Integer>)List.class);
Why do the declarations of tListInt2
and tListGeneric
give and error?
Why does upcast and then downcast with tListInt3
not produce an error?
Is there a better way to declare tListInt3
?
With kind regards,
Kasper van den Berg
ps. Let me know if you like to see code the outer generic container that needs this type information; i'll post it if needed.