0

I have two methods.

void test1(Object o1, AnotherObject o2) {
    call("test1(o1, o2)");
}

void test2(MyObject 01, MyAnotherObject o2, MyNextObject 03) {
    call("test2(o1, o2, o3)");
}

I want to invoke these methods with same parameters from another method. For example,

void call(String functionStr) {
    // invoke method via functionStr
    // if for test1, invoke -> test1(o1, o2);
    // if for test2, invoke -> test2(o1, o2, o3);
}

void main() {
    call("test1(o1, o2)");
    call("test2(o1, o2, o3)");
}

Please help me how I can call these functions from passed string?

Thanks in advance.

Tun Lin Aung
  • 289
  • 1
  • 2
  • 17
  • 1
    https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string – OneCricketeer Jul 24 '18 at 04:24
  • You can't. You are just passing ```String``` how can you get ```MyObject``` or ```MyAnotherObject```? – zhh Jul 24 '18 at 04:25
  • I want to call same recursive method from caller function. But don't have idea except passing string and reflect it and invoke. any idea? – Tun Lin Aung Jul 24 '18 at 04:30
  • You do not have any instance of the parameters `o1`, `o2` and `o3`. No one is able to magically create them out of nowhere. – Jai Jul 24 '18 at 05:02
  • You could if you go with `call(String method, Object... args)` instead. You won't be able to get the values of `o1`, `o2`, `o3` from a`String`. – AxelH Jul 24 '18 at 05:48
  • Provide some details of your target please. What **exactly** do you wanna achieve? List some demos if possible. – Hearen Jul 24 '18 at 06:17

0 Answers0