If you have a method with the signature:
Class<? extends List<String>> getObjectType()
{
return ?????;
}
How do you return a proper generic version of the List class?
return List.class; //errors
return List<String>.class; //errors
return List.class<String>; //errors
what is the proper syntax to handle this?
From the book "Effective Java" (Second Edition, page 137): "Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in their client code."
That said, to cast, you must first create a temporary variable:
This works because you can have annotations on assignments. It doesn't work on
return
itself. You could also add that to the method but that would hide other problems as well.List.class and List<String>.class - in all this cases you need casting. Really you need a type, that has List<String> at runtime, somethig like this:
You need to explicitly cast it to the return type. This works:
Yes it just looks wrong. This is just one of the many reasons Java's generics system is a mess.
You have to return the class of something that extends
List<String>
.ArrayList<String>
is one example. You can try: