|
|
||||||
|
#1
|
|
|
|
|
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 |
|
|
|
#2
|
|
|
|
|
"fAnSKyer" <fanskyer> wrote in message
news:7100 >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/t...ons.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. |
|
#3
|
|
|
|
|
fAnSKyer wrote:
> 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. |
|
#4
|
|
|
|
|
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... |
|
#5
|
|
|
|
|
"EJP" <esmond.not.pitt> wrote in message
news:1960 [..] > > 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. |
|
|
| Similar Threads | |
| How to retrieve a generic method with GetMethod (reflection) ? How to retrieve a generic method with GetMethod Code ------------------- #public static void Sort<T>( T[] array, Comparison<T> comparison [System.Reflection.MethodInfo]... |
|
| Reflection : GetEvent, GetMethod, Bind Method on Event TValue oItem = (TValue)Activator.CreateInstance(typeof(TValue), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { m_nCleTemporaire... |
|
| GetMethod? Hi I saw this fragment code somewhere. MethodInfo mi=dg.GetType().GetMethod("get_DataGridRows"); I know that that code gets info about get_DataGridRows method. but I can't... |
|
| GetMethod Hi all, when using the GetMethod method to discover object methods at runtime, we are suposed to pass an array of types as parameters. My simple question is this: how do... |
|
| Type.GetMethod("GetMethod") does work Hi, I have a Type reference and I want to actually get the MethodInfo for the method GetMethod, but it comes back as null. I am assuming that GetMethod works on the class... |
|
|
All times are GMT. The time now is 01:24 AM. | Privacy Policy
|