When T doesn’t Match Itslef

Thats a true story.
If a method returns you a List<T> doesn’t mean that you can guarantee getting a List<T>, it might do, and might not. Not even a subtype of T, actually you have to declare that T is covariant explicitly.

class Foo{
}
class Bar extends Foo{
}
List<Foo> foos = new ArrayList<Bar>(); // doesn't compile
List<? extends Foo> foos = new ArrayList<Bar>(); // compiles

But thats fine, the problem is that when T doesn’t match itself either, for ex:

static <T> List<T> getTs(){
   String[] strings = {"type","system"};
   List<T> list = new ArrayList<>();
   for (String string : strings) {
        list.add((T)string);
   }
   return list;
 } 

List<Foo> foos = getTs(); // compiles fine, runs fine

// problems happen here
//java.lang.ClassCastException: java.lang.String cannot be cast to Foo
for (Foo foo : foos) { 
     System.out.println(foo);
}

And btw, this code is not so artificial, here is a real world example. Gson API for JSON parsing,

static <T> List<T> getTs(){
  Type type = new TypeToken<List<T>>() {}.getType();
  List<T> list = new Gson().fromJson("[{id:1}]" , type);
  return list;
}
List<Foo> foos = getTs(); // fine, but doesn't contain Foos
Advertisements

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

You may use these HTML tags and attributes:

<a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: