Hi,
I use a arraylist to store parameters for a perticular method, which is
going to be invoked via reflection. The method maybe defined in any
number of parameters, or be defined in varargs, which I cannot ensure
at design time. How can I use my parameter arraylist to invoke the
methods? Thank you!
ricky.clarkson@gmail.com - 12 Dec 2005 13:48 GMT
Reflection is usually a waste of effort, you'd be better off using
normal Java or going the whole hog to a dynamically-typed language.
Reflection loses static type safety, and doesn't gain the apparent
advantages of dynamic languages either.
Anyway,
You can see whether a Method is var args, using the rather cryptic
method:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html#isVarArgs()
So if (that's true) method.invoke(new Object[]{arrayList.toArray()});
else method.invoke(arrayList.toArray());