Advertisement
RehabCZ

Java Reflection Method Invoke

Jan 23rd, 2024
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | Source Code | 0 0
  1.     public static Object genericInvokeMethod(Object obj, String methodName, Object...params) {
  2.         int paramCount = params.length;
  3.         Method method;
  4.         Object requiredObj = null;
  5.         Class << ? > [] classArray = new Class << ? > [paramCount];
  6.         for (int i = 0; i < paramCount; i++) {
  7.             classArray[i] = params[i].getClass();
  8.         }
  9.         try {
  10.             method = obj.getClass().getDeclaredMethod(methodName, classArray);
  11.             method.setAccessible(true);
  12.             requiredObj = method.invoke(obj, params);
  13.         } catch (NoSuchMethodException e) {
  14.             e.printStackTrace();
  15.         } catch (IllegalArgumentException e) {
  16.             e.printStackTrace();
  17.         } catch (IllegalAccessException e) {
  18.             e.printStackTrace();
  19.         } catch (InvocationTargetException e) {
  20.             e.printStackTrace();
  21.         }
  22.  
  23.         return requiredObj;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement