Posted By:
p_velasco
Posted On:
Tuesday, April 26, 2005 08:25 AM
Hi, I want to invoke a method (by using reflection) that requires an array of string as arguments. I have this example: public class Test { public String getMsg(String [] s) { return s[0]; } public int add(int a, int b) { return a + b; } public static void main(String args[]) { try { Class cls = Class.forName("util.Test"); Class partypes[] = new Class[2]; partypes[0] = Integer.TYPE; partypes[1] = Integer.TYPE; Method meth = cls.getMethod( "add", partypes); Test c = new Test(); Object arglist[] = new Object[2]; arglist[0] = new Integer(37); a
More>>
Hi,
I want to invoke a method (by using reflection) that requires an array of string as arguments. I have this example:
public class Test {
public String getMsg(String [] s) {
return s[0];
}
public int add(int a, int b) {
return a + b;
}
public static void main(String args[]) {
try {
Class cls = Class.forName("util.Test");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Method meth = cls.getMethod( "add", partypes);
Test c = new Test();
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = meth.invoke(c, arglist);
Integer retval = (Integer)retobj;
System.out.println(retval.intValue());
} catch (Throwable e) {
System.err.println(e);
}
}
}
How can modify this code for running the
public String getMsg(String [] s)
method?
<<Less