Re: getmethod in reflection
"fAnSKyer" <fanskyer@gmail.com> wrote in message
news:1161721452.221625.257100@i3g2000cwc.googlegroups.com...
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.