Dynamic method calls
Dear All
I am trying to use dynamic method calls, and have written the below code,
but not getting any results. Could you please help to point out my mistakes?
Thank you in advance
Denis
// ------------------------------------- My code -------------------------
import java.lang.reflect.*;
import java.text.*;
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
TestClass cartrec = new TestClass();
cartrec.solvethis();
} // End main
public void solvethis () {
Object myres=0 ;
String Functionstest[][]= {
{"Mult1","1","2","3"},
{"Concat1","Good","morning"},
{"Date1Before","7 7 2004","8 8 2005"}
};
// To invoke the Mult1 method
//---------------------------------------------------------------
Class c = this.getClass();
Class[] parameterTypes = new Class[Functionstest[0].length];
Object[] parameters = new Object[Functionstest[0].length];
try {
for (int j = 0; j < Functionstest[0].length - 1; j++) {
parameterTypes[j] = Integer.TYPE;
parameters[j] = new Integer(Functionstest[0][j+1]);
}
Method m = c.getMethod(Functionstest[0][0], parameterTypes);
myres = m.invoke(this, parameters);
} // End Try
catch (IllegalAccessException e) { }
catch (InvocationTargetException e) { }
catch (NoSuchMethodException e) { }
System.out.println("The result is = " + myres);
} // end solve this
// -----------------------------------------------------------------------
public int Mult1(int Var1,int Var2, int Var3)
{
int result;
result= Var1 * Var2 * Var3;
return result;
}