Re: Question about java method calling
Re: Question about java method calling
- Subject: Re: Question about java method calling
- From: Kevin Xiaowen Ren <email@hidden>
- Date: Thu, 07 Oct 2004 13:37:33 +1300
Thanks, Fabrice, that's what I want, Reflection...
At 02:13 7/10/2004 +0200, you wrote:
On 7 oct. 04,
at 00:30, Kevin Xiaowen Ren wrote:
Could I know the code for
" The default implementation of WODirectAction's performActionName()
appends "Action" to the method's argument. "?
I am trying to call some dynamic method by method name string in class.?
Well, since we don't have the WebObjects source we have to write
something by ourself...
Finding the method `name` for the class `clazz` :
import
java.lang.reflect.*;
Method
findMethod (String name, Class clazz)
{
Method[]
methods = clazz.getMethods ();
for
(int loop = 0; loop < methods.length; ++ loop)
{
if (methods[loop].getName ().equals (name)) return method;
}
return null;
}
Method invocation :
Object target = ...
Method method = findMethod ("myMethodByName", target.getClass ());
Object result = method.invoke (target, new Object[] { `method arguments` })
Pretty easy, isn't it ? Reflection is a very powerful feature, but can be costly so be careful. :)
--
Fabrice Truillot
Men in Silicium Ltd. - ã±ã¤ç´ ã®äºº </blockquote></x-html>
Kevin Xiaowen Ren
Programmer
Black Albatross
64-3- 479 5468
www.blackalbatross.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden