ArrayList to Varargs

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

=?iso-8859-1?B?bW9vcJk=?=

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!
 
R

ricky.clarkson

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());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Varargs 4
varargs function, is this legal? 4
Help in hangman game 1
Interface design - options with varargs 9
Tasks 1
ArrayList called with specific object constructors 20
type-safe varargs 13
Sorting an ArrayList 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top