Here you can find the source of invokeMethod(Class> clazz, Object object, String methodName, Class>[] parameterTypes, Object[] parameters)
public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters)
//package com.java2s; /*/*from w w w.ja va2 s . c om*/ * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with ZXC.com. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters) { Method method = getDeclaredMethod(clazz, methodName, parameterTypes); if (method == null) { throw new RuntimeException("Method `" + methodName + "` not found in class `" + clazz.getName() + "`."); } if (!method.isAccessible()) { method.setAccessible(true); } try { return method.invoke(object, parameters); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } public static Method getDeclaredMethod(Class<?> clazz, String methodName) { try { Method foundMethod = null; Method[] methods = clazz.getDeclaredMethods(); for (Method m : methods) { if (methodName.equals(m.getName())) { if (foundMethod != null) { throw new RuntimeException("Found two method named: " + methodName + " in class: " + clazz); } foundMethod = m; } } if (foundMethod == null) { throw new NoSuchMethodException("Cannot find method named: " + methodName + " in class: " + clazz); } return foundMethod; } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { if (clazz == Object.class) { return null; } else { return getDeclaredMethod(clazz.getSuperclass(), methodName); } } } public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) { try { Method method = clazz.getDeclaredMethod(methodName, parameterTypes); return method; } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { if (clazz == Object.class) { return null; } else { return getDeclaredMethod(clazz.getSuperclass(), methodName, parameterTypes); } } } }