Here you can find the source of invokeMethod(Object obj, String methodName, Object... args)
public static Object invokeMethod(Object obj, String methodName, Object... args)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static Object invokeMethod(Object obj, String methodName, Object... args) { try {/*from w w w . j a v a2 s. c o m*/ Method m; if (args != null) { Class<?>[] argClasses = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) argClasses[i] = args[i].getClass(); m = obj.getClass().getMethod(methodName, argClasses); return m.invoke(obj, args); } else { m = obj.getClass().getMethod(methodName); return m.invoke(obj); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } throw new RuntimeException("Error!"); } public static Class<?> getClass(String className) { try { if (className == null) return null; else return Class.forName(className); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("Error in instantiating class: " + className); } } }