I am trying to create a collecion of objects using reflection. I have the class name. But at compile time have no idea about the constructor. I used this tutorial.
import java.lang.reflect.*;
public class constructor2 {
public constructor2()
{
}
public constructor2(int a, int b)
{
System.out.println(
"a = " + a + " b = " + b);
}
public static void main(String args[])
{
try {
Class cls = Class.forName("constructor2");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
}
catch (Throwable e) {
System.err.println(e);
}
}
}
Now let's say I don't know abut the parameters of the constructors. (Whether it's a default constructor or two integers). The costructor parameters can change from class to class. I have no idea about the Constructor2 class. But I have the className. So I cant create the "argList" as above. Any idea how I can create a object from constructors then.
I did as follows
Constructor[] brickConstructorsArray = brickClass.getConstructors();
for (Constructor constructor : brickConstructorsArray) {
Class[] constructorParams = constructor.getParameterTypes();
Object argList[] = new Object[constructorParams.length];
int index = 0;
for (Class cls : constructorParams) {
if (cls.equals(Sprite.class)) {
Object spriteOb = foundSprite;
argList[index] = spriteOb;
} else if (cls.equals(double.class)) {
Object dblObj = new Double(0.0);
argList[index] = dblObj;
}
index++;
}
brickObject = (Brick) constructor.newInstance(argList);
}
This would work for costrcutors which will have Sprite or double parameters. To include other possibilities I'd have to create a loooong if else chain. Need help.