• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics collections and reflection

 
Franc Crosses
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I haven't tried compiling it, but can you try
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and now I see I have answered the wrong question. One sec, digging deeper...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

like this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic