Efficient way of dynamically invoking a method that returns a primitive type?

Discussion in 'Java' started by Andreas Jakobik, Dec 8, 2004.

  1. Hi,

    is there an efficient way to dynamically invoke a method that returns
    a primitive type? I'd like to avoid the Object to primitive type
    conversion. The temporary object wrapper should not have to be created
    at all.

    In our application (a Configuration Service within a telcom platform)
    we dynamically call methods on so called Managed Objects (MOs). Such
    an MO may implement the folloing method:

    public int getPmX();

    The configuration service calls this method by means of reflection,
    i.e. Method.invoke(...). However the returned value is (of course) an
    Object which must be casted.

    My question is: If a client application has a reference to an MO, and
    knows the name of the method (but only in run-time) and that it
    returns an int (as in the above example), is there a better way to
    call the method than using Method.invoke(). My assumption is no... (At
    compile time the client application does not know the method name.)

    BTW, we're on a J2ME CDC foundation profile which provides an Java 1.2
    API.

    Thanks a lot!

    /Andreas
     
    Andreas Jakobik, Dec 8, 2004
    #1
    1. Advertising

  2. Re: Efficient way of dynamically invoking a method that returns aprimitive type?

    Andreas Jakobik wrote:
    > is there an efficient way to dynamically invoke a method that returns
    > a primitive type? I'd like to avoid the Object to primitive type
    > conversion. The temporary object wrapper should not have to be created
    > at all.


    No, there is no way to do this. It's a meaningless premature optimization,
    since you're already being so inefficient by using reflection that it
    doesn't matter.
     
    Michael Borgwardt, Dec 8, 2004
    #2
    1. Advertising

  3. Andreas Jakobik

    Chris Uppal Guest

    Andreas Jakobik wrote:

    > is there an efficient way to dynamically invoke a method that returns
    > a primitive type? I'd like to avoid the Object to primitive type
    > conversion.


    Well, if you /really/ think its worthwhile, then you can do it very efficiently
    by generating a forwarding class on-the-fly. You'd probably want to use a
    byetcode library like BCEL, ASM, or the GNU Bytecode Library, to handle the
    tedious bits of bytecode generation.

    I doubt /very/ much whether it's worth it, though...

    -- chris
     
    Chris Uppal, Dec 8, 2004
    #3
  4. I suppose you have a very good reason why the client application does
    not know the NAME of the method until runtime? smells like a bad
    versioning problem...
    There is Almost always a way to know the name of the method at compile
    time. that is, assuming that you are writing both the client and server
    software...
     
    sanjay manohar, Dec 9, 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. garfield
    Replies:
    3
    Views:
    486
    John C. Bollinger
    Oct 30, 2003
  2. DaKoadMunky
    Replies:
    8
    Views:
    3,330
    Leor Zolman
    May 14, 2004
  3. Replies:
    7
    Views:
    614
    Victor Bazarov
    May 9, 2005
  4. Replies:
    2
    Views:
    432
  5. Daniel Pitts
    Replies:
    7
    Views:
    489
Loading...

Share This Page