Hi to all,
I have a simple class, i.e
public class TestRun { private Set<Integer> set; private String string; public List<String> collection; private HashSet<Boolean> hs; ... }
I want to make a factory for this class, so that each collection will be instantiated to a subclass I will define:
Factory call:public class BusinessObjectFactory { public static Object create(Class<?> c) throws InstantiationException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException { Object o = (Object) c.newInstance(); for ( Field field : c.getDeclaredFields() ) { Class<?> fieldClass = field.getType(); Object fieldObject = null; if(fieldClass.getName().equals("java.util.Set")) { fieldObject = new HashSet(); } else if(fieldClass.getName().equals("java.util.List")) { fieldObject = new ArrayList(); } else if(fieldClass.getName().equals("java.util.Map")) { fieldObject = new HashMap(); } else { fieldObject = fieldClass.newInstance(); } field.setAccessible( true ); field.set( o, fieldObject); } return o; }
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