getmethod in reflection

F

fAnSKyer

I want use getmethod to get an already known method and invoke it.

The problem is, I don't know the parameters of this method, what I have
is an Object[] array that contains the parameters. So how to solve this
problem?

Sorry for this silly question,
Thanks a lot,
fAnS
 
M

Mike Schilling

fAnSKyer said:
I want use getmethod to get an already known method and invoke it.

The problem is, I don't know the parameters of this method, what I have
is an Object[] array that contains the parameters. So how to solve this
problem?

You will need to do something similar to what the compiler does:

1. Find all the candidate methods, that is, all the methods with the right
name declared by the class and its superclasses. The compiler does this
based on the declared type of a reference; I'm guessing that you'll do it
based on the actual type of the object referred to. If I'm wrong, and you
happen to be looking for a method defined on an abstract class, remember to
look at the interfaces the class implements (and their superinterfaces) as
well as the superclasses.

2. Do whatever makes sense in your case about protection. If you know the
method is public, eliminate the others, etc.

3. For each method, see if the parameters you're given make sense. E.g. if
the method takes a String as its first parameter, eliminate it. If the
method takes three parameters and you have four, eliminate that too. (If
you're allowing variable-argument methods, this gets a bit more complex.)

4. If more than one remains, apply Java's arcane rules about preferring the
most specific method (see
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12)

Hopefully, there's exactly one candidate method left. Call it. If there
are either none or more than one left, throw an exception.
 
E

EJP

fAnSKyer said:
I want use getmethod to get an already known method and invoke it.

The problem is, I don't know the parameters of this method, what I have
is an Object[] array that contains the parameters. So how to solve this
problem?

Here's the easy way:

1. If the method doesn't return a value:

import java.beans.Statement;

Statement statement = new Statement(target, "method", params);
statement.execute();

2. If the method returns a value:

import java.beans.Expression;

Expression expr = new Expression(target, "method", params);
Object result = expr.getValue();

These both do exactly what the compiler does in terms of looking up the
method and matching the parameter types.
 
C

Chris Uppal

EJP wrote:

Expression expr = new Expression(target, "method", params);
Object result = expr.getValue();

These both do exactly what the compiler does in terms of looking up the
method and matching the parameter types.

According to the code (but not the documentation, as far as I can see)
Statement and Expression are limited to public methods, and perhaps also to
public classes. Which, if true[*], means that not only may they fail where
"real" code would work, but they may also resolve to different methods.

That /may/ matter to the OP.

-- chris

[*] the code vanishes off into Sun's private stuff where I couldn't be bothered
to follow. Similarly I couldn't be bothered to write a simple test. I'm
feeling /so/ lively today...
 
M

Mike Schilling

EJP said:
fAnSKyer said:
I want use getmethod to get an already known method and invoke it.

The problem is, I don't know the parameters of this method, what I have
is an Object[] array that contains the parameters. So how to solve this
problem?

Here's the easy way:

1. If the method doesn't return a value:

import java.beans.Statement;

Statement statement = new Statement(target, "method", params);
statement.execute();

2. If the method returns a value:

import java.beans.Expression;

Expression expr = new Expression(target, "method", params);
Object result = expr.getValue();

These both do exactly what the compiler does in terms of looking up the
method and matching the parameter types.

Cool. I didn't know about these.
 

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


Members online

No members online now.

Forum statistics

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

Latest Threads

Top