posted
I don't think this is possible - I couldn't find it anywhere I looked - but nothing lost from asking.
Does anyone know if there is a way to get the generic type of a collection in java?
What I mean is, let's say I have a class with a property ArrayList<T> that I instantiate and set the ArrayList as say an ArrayList<String>. Is there anyway to get the String class of this array list? I figured this is impossible because the generics are erasures, so there's no way to get information about it during run time, but it'd be so much easier if I could get this work.
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
posted
I don't believe there is any way to know at run-time.
I'm trying to imagine why you need to know what the type of the collection was to think of a work-around. Would inspecting the elements of the list to get their types not be sufficient?
You know, like:
Object o = list.get(0); System.out.println(o.getClass());
Posts: 5656 | Registered: Oct 1999
| IP: Logged |
posted
Yep, type erasure makes it impossible in the general sense. There are, however, several options outside of that. Without knowing your particular situation, I can't know if any is appropriate.
1) Re-architect the code so this isn't an issue. Most of the time, when you find yourself wanting to do this sort of thing, it means someone did a bad job of designing some code.
2) Make sure all the possible classes are subclasses/subinterfaces with specific types that are derived from generics with specific type parameters. MyStringList extends ArrayList<String> and all that. This is hacky, but keeps things typesafe.
3) Less hacky, if you know the type information at some point, is using the type inside of another type, and parameterizing by that. That is, inside MyWrapper<ItemType> instantiate ArrayList<ItemType>.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Wouldn't that be prone to failure if it was declared with a base class, but if the first element was a child class.
In other words, if you had something like ArrayList<BaseClass> and you inserted an instance of ChildClass, then ran Xavier's code, what would the output of o.getClass() be. I'm not too familiar with Java, but I suspect it would be ChildClass, when you really wanted BaseClass.
Posts: 2437 | Registered: Apr 2005
| IP: Logged |
posted
I trying to do a quick set of Collections bindings. I've got a bunch of utilities and gui classes that I use with my custom designed Data Binding interfaces and I was trying to get some no fuss ways of adding and editing items with a collection.
Really, this isn't that big a deal. My other solution is to require a class to be set for the binding, because I can't ensure that the collections I dealing with won't be empty, so I can't pull it from there.
It's just that it'd be easier and cooler to just pass in a typed collection and have it know that that is the class.
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
posted
You can always just throw the question up on a Q&A site like Stackoverflow and see if anything comes of it.
Posts: 2437 | Registered: Apr 2005
| IP: Logged |
posted
Yeah, I'd go with my third listing up above: parameterize the wrapper. So you'd do things like Binding<T>.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Knowing the type of the empty collection is very important if you have a binding to add a new item to a collection.
The main thing I need this for is generating new objects of the collection type, so I decided to go with putting a class property on the new item binding.
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
posted
Just a tip - if you need an answer to any programming related question, go here: www.stackoverflow.com.
If you can't find a question similar to yours then you can ask your question and get an answer in a matter of minutes.
Edit: And apparently I'm slow and didn't see that someone else had already posted it. In any event - I second the suggestion to ask Stack Overflow.
Posts: 3295 | Registered: Jun 2004
| IP: Logged |