I have a problem involving Generics. Here's a simplified version:
Suppose I have a generic method to add a new element to a (possibly empty) ArrayList, like this:
<T> void addNewTo(ArrayList<T> list)
What I need to do is to determine the correct class for the ArrayList elements so as to create the right kind of new element. Ie, I really want to say
<T> void addNewTo(ArrayList<T> list) {
list.add(new T());
}
However, because of the way Java implements generics via type erasure, you can't do that. There are some really ugly work-arounds for the general case of implementing "new T()", but maybe there's a better way in this particular case?
I'd be very grateful for any ideas or suggestions.
Thanks,
J