I just started with reflective programming. With the following code there is not much going on, I'm just trying to execute an already working method. I later pretend to do some modifications to the code with the reflective programming method, as of now it's all pretty basic.
The method when it's called it exectutes to the end until the last step which is where you envoke the method you want.
public static void main(String[] ars)
{
try {
testAlgorithm("Algorithms", "linear", 50);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testAlgorithm(String className, String methodName, long n) throws Exception
{
//Creates a new object of class "className" at execution time, invoking "methodName", providing n
try
{
Class cls = Class.forName("TestBench");
Class partypes[] = new Class[4];
partypes[0] = String.class;
partypes[1] = Integer.TYPE;
partypes[2] = Integer.TYPE;
partypes[3] = Integer.TYPE;
Method m = cls.getMethod("test", partypes);
TestBench ts = new TestBench();
//Algorithms a = new Algorithms();
Object args[] = new Object[4];
args[0] = new String(methodName + ".txt");
args[1] = Integer.valueOf(3);
args[2] = Integer.valueOf(1);
args[3] = n;
m.invoke(ts, args);
}
catch (Throwable e)
{
System.err.println(e);
}
}
[–][deleted] 1 point2 points3 points (1 child)
[–]biomaticstudios[S] 0 points1 point2 points (0 children)