Call a method dynamically
Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html
Given the class name and function name, call function using reflection API.
import java.lang.reflect.*;
import java.io.*;
public class testReflect {
public static void main(String s[]) {
testReflect t = new testReflect();
try {
t.doit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void doit() throws Exception {
String aClass;
String aMethod;
// we assume that called methods have no argument
Class params[] = {};
Object paramsObj[] = {};
while (true) {
/* examples
Class: class1 Method: class1Method2
Class: java.util.Date Method: toString
Class: java.util.Date Method: getTime
*/
aClass = Input.Line("\nClass : ");
aMethod = Input.Line("Method: ");
// get the Class
Class thisClass = Class.forName(aClass);
// get an instance
Object iClass = thisClass.newInstance();
// get the method
Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
// call the method
System.out.println(thisMethod.invoke(iClass, paramsObj).toString());
}
}
static class Input {
public static String Line(String s) throws IOException {
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
System.out.print(s);
return input.readLine();
}
}
}
class class1 {
public String class1method1() {
return "*** Class 1, Method1 ***";
}
public String class1method2() {
return "### Class 1, Method2 ###";
}
}
Posted On: 4-Jul-1999

Click Here!
Get the Answers You Need!
Incentive-based, question & answer Marketplace.
The first 5,000 members receive $12 credit!
Try it!
Knowledge Market.

For tons of Windows technology resources, check out the Windows Programming directory.

Target your Technical searches with TechCrawler

Sweepstakes:
Here's your chance to win Windows
2000 Professional. EarthWeb's sweepstakes runs through July 2. Click here to enter.


Click Here!
|