Invoking variable method with var args via reflection

F

Faiser

Hello,

I am having trouble invoking a method that accepts a variable amount
of parameters via run time reflection. I locate the method a
signature of the method with:

Method m = targetClass.getDeclaredMethod( "methodName",
ArgType[].class )

Where the method declaration looks like:

public ReturnObj methodName( ArgType .... args ) { /* method body....
/* }


This works fine. At run time, however, I try to invoke the method
with zero arguments with:

ReturnObj o = (ReturnObj)m.invoke( target );

Where target is an instantiated target class, m is the method from
above, and the return value is of the correct type.

This unfortunately results in the following exception: "Exception in
thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong
number of arguments".

Practically I assume I could solve this problem by passing in a zero
lengthed array, or even a null value. I am curious though to see if
any one else has found a more consistent i.e. elegant solution. Would
anyone who's encountered this please offer any comments if they were
able to solve this in another way?

Thank you.
-Faiser
 
G

Gordon Beaton

Practically I assume I could solve this problem by passing in a zero
lengthed array, or even a null value. I am curious though to see if
any one else has found a more consistent i.e. elegant solution.

From the API documentation for Method.invoke():

If the number of formal parameters required by the underlying method
is 0, the supplied args array may be of length 0 or null.

So if you don't like passing a zero-length array, pass null.

/gordon
 
C

Chris Uppal

Faiser said:
I am having trouble invoking a method that accepts a variable amount
of parameters via run time reflection. I locate the method a
signature of the method with:

Method m = targetClass.getDeclaredMethod( "methodName",
ArgType[].class )

Where the method declaration looks like:

public ReturnObj methodName( ArgType .... args ) { /* method body....
/* }


This works fine. At run time, however, I try to invoke the method
with zero arguments with:

ReturnObj o = (ReturnObj)m.invoke( target );

But the method (the real method that the JVM sees, not the fake that the Java
compiler pretends is there) takes exactly one parameter -- an array -- as you
recognise when you are finding it using reflection. Why do you think it's OK
to invoke it differently ? It takes exactly 1 argument. so you should provide
exactly 1 argument. No ?

-- chris
 
F

Faiser

But the method (the real method that the JVM sees, not the fake that the Java
compiler pretends is there) takes exactly one parameter -- an array -- as you
recognise when you are finding it using reflection. Why do you think it's OK
to invoke it differently ?

That was exactly my initial thought. I starting doubting that though
when the following through an IllegalArgumentException exception:

ReturnObj o = (ReturnObj)m.invoke( target, new ArgType[0] );

Surely this was accurate, I though; how else could the method be
signatured? Turns out that the compiler warnings evidently offer some
good advice. After sifting through and interpreting them, I used:

ReturnObj o = (ReturnObj)m.invoke( target, (Object)new ArgType[0] );

This ran without issue and is the current implementation.

Thanks Gordon & Chris for the responses.




Chris Uppal said:
Faiser said:
I am having trouble invoking a method that accepts a variable amount
of parameters via run time reflection. I locate the method a
signature of the method with:

Method m = targetClass.getDeclaredMethod( "methodName",
ArgType[].class )

Where the method declaration looks like:

public ReturnObj methodName( ArgType .... args ) { /* method body....
/* }


This works fine. At run time, however, I try to invoke the method
with zero arguments with:

ReturnObj o = (ReturnObj)m.invoke( target );

But the method (the real method that the JVM sees, not the fake that the Java
compiler pretends is there) takes exactly one parameter -- an array -- as you
recognise when you are finding it using reflection. Why do you think it's OK
to invoke it differently ? It takes exactly 1 argument. so you should provide
exactly 1 argument. No ?

-- chris
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top