Article:
  Lightweight R/O Mapping
Subject:   Generics and reflection don't mix?
Date:   2005-12-08 08:36:34
From:   SeanReilly
From the article:

The compiler will issue a warning here, since the reader only knows at runtime what kind of class it is going to create. Because of type erasure in the generics implementation of J2SE 5.0, it is not possible to safely cast the result. And, sadly, it is also not possible to write the BeanReader class as BeanReader<Jedi> for the same reason. In short, using Java reflection and generics don't mix.


Shame on you! This is not an example of reflection and generics not mixing. Instead, it's an example of the author not programming generics properly, which would be forgiveable, except that he is writing an article about generics. Sir, your knowledge of the subject matter at hand is so poor that I'm upset with the editor (if there even was one) that let this article through!


For the record, the correct way to write this kind of thing is:


BeanReader<Class<Jedi>> reader = new BeanReader<Class<Jedi>>(Jedi.class, connector);


Collection<Jedi> jediList = reader.executeCreateBeanList("blah");


This compiles perfectly properly without any compiler warnings.