I have java classes with different kind of methods. No parameter is needed to invoke some methods. Some methods takes 1 string parameter and some takes 1 string, 1 int params and etc. My point is there are different kind of methods as I told at the beginning. I put a dummy class above to make it easier to understand.
Is it possible to call random method if I have package/class/method name and parameters with reflection?
package dummyPackage;
public class DummyClass
{
public DummyClass() {
super();
// TODO Auto-generated constructor stub
}
public void foo() {
System.out.println("foo called");
}
public void bar(int sth) {
System.out.println(sth++);
}
public void doSth(int a,String b) {
System.out.println(a + "|" + b);
}
...
}
And below is showing what I need. I'm so new to java and have no idea how to solve this.
Object noparams[] = {};
Object[] params1 = new Object[1];
params1[0] = 100;
Object[] params2 = new Object[2];
params2[0] = 200;
params2[1] = "Test";
//What I need
NeededClass.reflect("dummyPackage.dummyClass.foo",noparams);
NeededClass.reflect("dummyPackage.dummyClass.bar",params1);
NeededClass.reflect("dummyPackage.dummyClass.doSth",params2);