Last active
December 13, 2015 22:05
-
-
Save antoni/04a8ec763c5038abffe9 to your computer and use it in GitHub Desktop.
Invoke a Java method when given the method name as a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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