Skip to main content
simplified code
Source Link
Martin Seeler
  • 7k
  • 3
  • 36
  • 46

You can use the ReflectionAPI to achieve this. Just get all methodsthe method with this name from your desired class you wish to invoke(if available ) and compare theire namesinvoke it with your stringargs, in this case null.

BUT it's a bad design and you should rethink your application flow!

here is an example:

public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); String[] calls = { "def", "abc", "ghi" }; try { Method[] methods = HelloWorld.class.getMethods(); for (String stringcall : calls) { for (Method method : methods) {  if= (methodHelloWorld.getName()class.equalsIgnoreCasegetMethod(string))call, {null);   method.invoke(hello, null); } } } } catch (Exception e) { // TODO: handle exception } } public void abc() { System.out.println("abc"); } public void def() { System.out.println("def"); } public void ghi() { System.out.println("ghi"); } 

}

BUT it's a bad design and you should rethink your application flow!

You can use the ReflectionAPI to achieve this. Just get all methods from your class you wish to invoke and compare theire names with your string.

here is an example:

public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); String[] calls = { "def", "abc", "ghi" }; try { Method[] methods = HelloWorld.class.getMethods(); for (String string : calls) { for (Method method : methods) {  if (method.getName().equalsIgnoreCase(string)) {   method.invoke(hello, null); } } } } catch (Exception e) { // TODO: handle exception } } public void abc() { System.out.println("abc"); } public void def() { System.out.println("def"); } public void ghi() { System.out.println("ghi"); } 

}

BUT it's a bad design and you should rethink your application flow!

You can use the ReflectionAPI to achieve this. Just get the method with this name from your desired class (if available ) and invoke it with your args, in this case null.

BUT it's a bad design and you should rethink your application flow!

here is an example:

public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); String[] calls = { "def", "abc", "ghi" }; try { for (String call : calls) { Method method = HelloWorld.class.getMethod(call, null); method.invoke(hello, null); } } catch (Exception e) { // TODO: handle exception } } public void abc() { System.out.println("abc"); } public void def() { System.out.println("def"); } public void ghi() { System.out.println("ghi"); } 

}

Source Link
Martin Seeler
  • 7k
  • 3
  • 36
  • 46

You can use the ReflectionAPI to achieve this. Just get all methods from your class you wish to invoke and compare theire names with your string.

here is an example:

public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); String[] calls = { "def", "abc", "ghi" }; try { Method[] methods = HelloWorld.class.getMethods(); for (String string : calls) { for (Method method : methods) { if (method.getName().equalsIgnoreCase(string)) { method.invoke(hello, null); } } } } catch (Exception e) { // TODO: handle exception } } public void abc() { System.out.println("abc"); } public void def() { System.out.println("def"); } public void ghi() { System.out.println("ghi"); } 

}

BUT it's a bad design and you should rethink your application flow!