Invoke constructor with unknown parameters

monimust

New Member
I've got set of classes: \[code\]TestClass1\[/code\], \[code\]TestClass2\[/code\] and \[code\]TestClass3\[/code\]. All those classes inherits from class \[code\]MainTestClass\[/code\]. Classes \[code\]TestClass1\[/code\], \[code\]TestClass2\[/code\] and \[code\]TestClass3\[/code\] has different constructors. For example: \[code\]TestClass1(String s)TestClass2(Integer i)TestClass3(Double d)\[/code\]Classes can have more than one constructor - each different from others.Now I want to randomly create instances of these classes and store them in List. To do that, I store \[code\]Class\[/code\] objects of \[code\]TestClass1\[/code\], ... using \[code\]TestClass1.class\[/code\], ... in a List. Then, I get for example first constructor of each class, that has non-zero parameters count. Now, when I want to create object using \[code\]newInstance()\[/code\] method, I don't know types and count of arguments to pass to this specific constructor.Is it possible to achieve something like this using reflection? I have been thinking about something like \[code\]setConstructorArgument(int number, Object value)\[/code\]or \[code\]setConstructorArguments(Object[] arguments)\[/code\]Do similar methods exists in Java?
 
Back
Top