• Post Reply Bookmark Topic Watch Topic
  • New Topic

running a method by it's string name  RSS feed

 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I know the method name of something I want to call is "org.blah.someclass.method()" -
How do I run that method with just the string name?
Or do I have to do a getClass for the object, and then run the method?
I think I just answered my question.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,

Say I know the method name of something I want to call is "org.blah.someclass.method()" -
How do I run that method with just the string name?

With Method.invoke().

Or do I have to do a getClass for the object, and then run the method?

Yep. Think about it. If the method is non-static then the side effects and return value of a method is dependant on the state of the particular instance.
Here's a quick example:

Michael Morris
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or with just the String name of the method:

Michael Morris
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here's the reason for this:
I'm building a modular web application, and with each module added, I want for that module to be able to have a static method run by the task scheduler.
I created a simple "task" object that sits in the ServletContext, that contains the string name of the class & method to execute, and the approx. time of execution.
The Windows Task Scheduler will run a bat program calling the task execution servlet, that will pull the task objects from the context and run the task if it is time.
So, each module's initialization section will define any scheduled tasks to be run. What would be the better method (as in way) of getting the tasks to the task executor? Simply by passing the String name of the method, or should I pass the class and method as objects?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

... What would be the better method (as in way) of getting the tasks to the task executor? Simply by passing the String name of the method, or should I pass the class and method as objects?

Unless the method is parameterless, you'll probably have to use the latter approach. How do you plan to pass the parameters to the method?
Michael Morris
[ March 18, 2003: Message edited by: Michael Morris ]
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:

Unless the method is parameterless, you'll probably have to use the latter approach. How do you plan to pass the parameters to the method?
Michael Morris
[ March 18, 2003: Message edited by: Michael Morris ]

I'm not sure, I know I'm going to need to pass at least one object...
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I'm not hard-coding in the class, so it can be variable, I was planning on passing that along too somehow.
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I think about it, I'll just make an abstract class and base all the objects I want to run off of that, I think I could get that working without too much fuss.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!