This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Your previous responses on this topic 'method invocation' has greatly helped me in writing a code to invoke method dynamically.As some experts pointed out,the parameters had to be type casted.
Please help me out of my latest trouble.I invoke only a particular method 'methodName' of a particular calss instance 'instance'.But when i run the code Exceptions are thrown for all the methods of that class whereas i require exceptions connected with methodName only.
I just could not figure out my mistake, so please help me out.
anitha, I asked "How did you call setParameters ?" Please send us the code where you are calling this method.
Joined: Jul 12, 2006
Yep.I am sorry i couldnt get you then.Heres the code where i get the method.It is quite a lengthier one.I choose JFileChooser to select the class file,upon which all the methods in that file will be displayed dynamically in a panel as radiobuttons.
The method is selected when i click a radio button. Hope you will understand my code.Since iam a novice it might seems clumsy to you.
THE FIRST ONE IS THE CODE TO CHOOSE A CLASS FILE AND DISPLAY METHODS.It also selects a method using radio button, action of whose is given below in class MethodRadioActionListener.
AND HERES THE CODE WHICH WORKS UPON SELECTING THE METHOD UPON CLICKING A RADIOBUTTON .IT DISPLAYS TEXTFIELDS FOR USER OUTPUT.
Joined: Jul 12, 2006
I tried various methods.The outputs are vague.the stackTrace is showing error in invocation,
I really dont understand what could be wrong with this.Please,if any one could trace it out do help me.
Well, first of all, think about what you're doing. You're calling a method that takes an "int" parameter. But your MethodInvoke.actionPerformed() method doesn't work with "int" parameters; it only works with Integer, String, Boolean, and a few other wrapper classes. For an int parameter the argument class will be Integer.TYPE, a special Class object that represents the primitive type int.
My advice to you is to start over completely from scratch. First, read up on the reflection API and study some samples. Don't try writing code until you understand how the API works.
Then start to write just the reflection code, without having it all tangled up in GUI code. Write a little bit, test that little bit, fix it, until it works. Then write a little more, test that bit, fix it, until it works. This is not the way to code -- writing a whole big pile that you barely understand and then trying to figure out what's wrong!
Start by writing a three-line program with a main() in which you call a specific method that takes an int parameter using reflection. Make sure that works. Then try a slightly longer one that takes the name of a method on the command line, but assumes there are no arguments. Then one that assumes there is one argument, and work out all the different possible argument types. Then add a loop so you can handle any number of arguments.