简体   繁体   中英

Java casting “.class”-operator used on a generic type, e.g. List, to “Class<List<?>>” and to “Class<List<Integer>>”

I use the .class -operator to supply information about the contained type to a generic class. For non-generic contained types, eg Integer.class , this works without any problems. But with the contained type being a generic, eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM