Java casting ".class"-operator used on a generic type, e.g. List, to "Class<List<?>>" and to "Class<List<Integer>>"
Asked Answered
M

1

17

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.

Mystery answered 21/9, 2011 at 15:14 Comment(1)
possible duplicate of How to create expressions of type Class<List<?>>Steelworker
C
27
Class<List<Integer>> tListInt3 = 
            (Class<List<Integer>>) ((Class<Integer>)List.class);

that doesn't work. you probably meant

Class<List<Integer>> tListInt3 = 
            (Class<List<Integer>>) ((Class)List.class);

we can always cast from one type to another by up-cast then down-cast

    Integer x = (Integer)(Object)"string";

The type of List.class is Class<List>; it is not a subtype/supertype of Class<List<Whatever>> therefore direct cast between the two types is illegal.

It can be argued that Class<List<Integer>> doesn't exist - there is only a class for List; there is no such class for List<Integer> (which really is just List at runtime)

However, this is a flaw of Java type system; in practice we do need things like Class<List<Integer>>. Our solution - casting and pretending Class<List<Int>> exits - is likewise flawed - but it's not our fault.

Collect answered 21/9, 2011 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.