This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes dynamic method invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "dynamic method invocation" Watch "dynamic method invocation" New topic

dynamic method invocation

anitha nair

Joined: Jul 12, 2006
Posts: 12
Hai forum,

I am a newbie to java and i am really confused about how to invoke method dynamically.Plz help me out with your suggestions and tips,which will be valuable to me.

I get the method selected dynamically and pass that to the method invocation class.The user inputs are collected in an array inside the class.

I have troubles in the following area:

1. whenever i run the following code MethodNotfound Exception is thrown for all the methods of the Classinstance, whereas invocation is required only for the methodName method which is passed.

2.I have no idea how to check for each parameter in the array for integer or float or double etc..and pass that to invoke method.

Please help me with your ideas.

Thank you all

Arafat Nalkhande
Ranch Hand

Joined: Jul 20, 2006
Posts: 51
I couldnt replicate your error, but following is the working code (with lots of hardcoding) that invokes the method using reflection.
Just check if this helps

I want to be myself, but a better myself.<br />~ SCJP 1.4 (91%), SCWCD 1.4 (86%) ~
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24190

The array of Class objects passed to getMethod() should be equal in length to the number of arguments of the method you want to call, and the Class arguments themselves should represent the types of those arguments. In your code, the number of Class objects is always equal to the number of TextFields, and they are always String.class. Therefore you'll only be able to find and call methods that have the same number of arguments as you have TextFields, and only if all the arguments are supposed to be Strings. Presumably few methods meet those criteria, so most of the time you'll get a MethodNotFoundException.

I don't see anything that suggests this code will try to invoke more than one method, but who knows what happens outside of the code you've shown us.

As far as your number 2: it's not at all an easy problem. Many of your argument Strings will be ambiguious: the String "27" could validly be an int, an Integer, a long, a Long, a Short, a short, a byte, a Byte, an Object, etc. So you have to start from the Method objects, find out what parameter types they accept, and then work backwards to see if the Strings you have can be converted to those types. And don't forget that methods can be overloaded: for any given method name, you would have to look at all the methods of that name and try to match their argument lists against the Strings you have to work with. In the general case, it's a fairly hard problem; expect to write a decent chunk of code to solve it.

Note that scripting environments for Java do exist, so if this has some purpose, as opposed to just being a learning exercise, you might want to try to embed one of them and use its existing code that does this. Check out "BeanShell", "Groovy", and "Jython", if you're interested.

[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here is an an interpreter I made to play with reflection. It has simple rules for inferring datatypes, but relies on the user to give hints at times. For example to indicate 27 is a String they'd have to type "27" with quotes. See Argument and Reflector for bits that may be of most interest.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: dynamic method invocation
It's not a secret anymore!