Skip to content

Instantly share code, notes, and snippets.

@antoni
Last active December 13, 2015 22:05
Show Gist options
  • Select an option

  • Save antoni/04a8ec763c5038abffe9 to your computer and use it in GitHub Desktop.

Select an option

Save antoni/04a8ec763c5038abffe9 to your computer and use it in GitHub Desktop.
Invoke a Java method when given the method name as a string
// Get the method
try {
java.lang.reflect.Method method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
// Invoke the method
try {
method.invoke(classObject, arg1, arg2,...);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment