Typically, I've seen people use the class literal like this:
Class<Foo> cls = Foo.class;
But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized:
Class<List> cls = List.class
So why not add a <?>
? Well, this causes a type mismatch error:
Class<List<?>> cls = List.class
I figured something like this would work, but this is just a plain ol' syntax error:
Class<List<Foo>> cls = List<Foo>.class
How can I get a Class<List<Foo>>
statically, e.g. using the class literal?
I could use @SuppressWarnings("unchecked")
to get rid of the warnings caused by the non-parameterized use of List in the first example, Class<List> cls = List.class
, but I'd rather not.
Any suggestions?
You could use a helper method to get rid of
@SuppressWarnings("unchecked")
all over a class.Then you could write
Other usage examples are
However, since it is rarely really useful, and the usage of the method defeats the compiler's type checking, I would not recommend to implement it in a place where it is publicly accessible.
To expound on cletus' answer, at runtime all record of the generic types is removed. Generics are processed only in the compiler and are used to provide additional type safety. They are really just shorthand that allows the compiler to insert typecasts at the appropriate places. For example, previously you'd have to do the following:
becomes
This allows the compiler to check your code at compile-time, but at runtime it still looks like the first example.
There are no Class literals for parameterized types, however there are Type objects that correctly define these types.
See java.lang.reflect.ParameterizedType - http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/ParameterizedType.html
Google's Gson library defines a TypeToken class that allows to simply generate parameterized types and uses it to spec json objects with complex parameterized types in a generic friendly way. In your example you would use:
I intended to post links to the TypeToken and Gson classes javadoc but Stack Overflow won't let me post more than one link since I'm a new user, you can easily find them using Google search
Well as we all know that it gets erased. But it can be known under some circumstances where the type is explicitly mentioned in the class hierarchy:
And now you can do things like:
More info here. But again, it is almost impossible to retrieve for:
where it gets erased.
Due to the exposed fact that Class literals doesn't have generic type information, I think you should assume that it will be impossible to get rid of all the warnings. In a way, using
Class<Something>
is the same as using a collection without specifying the generic type. The best I could come out with was:You can't due to type erasure.
Java generics are little more than syntactic sugar for Object casts. To demonstrate:
The only instance where generic type information is retained at runtime is with
Field.getGenericType()
if interrogating a class's members via reflection.All of this is why
Object.getClass()
has this signature:The important part being
Class<?>
.To put it another way, from the Java Generics FAQ: