• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

dynamic method invocation

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic