Invoking variable method with var args via reflection

Discussion in 'Java' started by Faiser, Aug 17, 2004.

  1. Faiser

    Faiser Guest

    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
    Faiser, Aug 17, 2004
    #1
    1. Advertising

  2. On 17 Aug 2004 09:21:53 -0700, Faiser wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Aug 17, 2004
    #2
    1. Advertising

  3. Faiser

    Chris Uppal Guest

    Faiser wrote:

    > 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
    Chris Uppal, Aug 18, 2004
    #3
  4. Faiser

    Faiser Guest

    > 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" <-THIS.org> wrote in message news:<>...
    > Faiser wrote:
    >
    > > 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
    Faiser, Aug 18, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    612
    Ken Varn
    Jun 22, 2005
  2. Pep
    Replies:
    5
    Views:
    12,430
    madala
    Jul 8, 2009
  3. Alex Vinokur
    Replies:
    4
    Views:
    464
    Ron Natalie
    Sep 24, 2003
  4. Replies:
    3
    Views:
    480
    David Eppstein
    Sep 17, 2003
  5. Keiron Waites

    var.innerHTML where var is a variable!!

    Keiron Waites, Aug 11, 2003, in forum: Javascript
    Replies:
    2
    Views:
    77
    Martin Honnen
    Aug 11, 2003
Loading...

Share This Page