Author
Generics collections and reflection
Franc Crosses
Greenhorn
Joined: Nov 29, 2010
Posts: 2
Hi to all,
I have a simple class, i.e
I want to make a factory for this class, so that each collection will be instantiated to a subclass I will define:
Factory call:
TestRun tr = (TestRun) BusinessObjectFactory.create(TestRun.class);
My question is:
How Have I to instantiate the collection with the right parameter:
new HashSet <Integer>(), new ArrayList <String >(), ...
I tried with fieldObject = new HashSet <?>(); ecc
but it' not possible...
Can someone help me please?
Thanks in advance
Francesco
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
posted Nov 29, 2010 08:24:44
0
sorry I haven't tried compiling it, but can you try
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
posted Nov 29, 2010 08:35:32
0
and now I see I have answered the wrong question. One sec, digging deeper...
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
posted Nov 29, 2010 08:47:00
0
I have asked this question before but have not received a answer yet.
If you have ClassA extends GenericClass<ClassB> you can sometimes get the type of ClassB from ClassA using:
((ParameterizedType ) fieldClass.getGenericSuperclass()).getActualTypeArguments()[0]
But the same does not work with plain old class of type GenericClass<ClassB>
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
And welcome to the Ranch
David O'Meara
Rancher
Joined: Mar 06, 2001
Posts: 13459
posted Dec 03, 2010 18:15:03
0
like this
subject: Generics collections and reflection