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

  • Thread starter Andreas Jakobik
  • Start date
A

Andreas Jakobik

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
 
M

Michael Borgwardt

Andreas said:
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.
 
C

Chris Uppal

Andreas said:
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
 
S

sanjay manohar

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...
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top