• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Question about java method calling
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Re: Question about java method calling (From: Kevin Xiaowen Ren <email@hidden>)
 >Question about java method calling (From: Kevin Xiaowen Ren <email@hidden>)
 >Re: Question about java method calling (From: Fabrice Truillot <email@hidden>)

  • Prev by Date: Re: Question about java method calling
  • Next by Date: Re: Can't load Application class, exiting...
  • Previous by thread: Re: Question about java method calling
  • Next by thread: Re: Question about java method calling
  • Index(es):
    • Date
    • Thread