Here you can find the source of invokeMethod(final Object instance, final String methodName, final Class>[] parTypes, final Object[] parameters)
public static Object invokeMethod(final Object instance, final String methodName, final Class<?>[] parTypes, final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
//package com.java2s; /******************************************************************************* * Copyright (c) 2012 SAP AG and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from ww w. j av a 2 s .co m*/ * SAP AG - initial API and implementation *******************************************************************************/ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class Main { public static Object invokeMethod(final Object instance, final String methodName, final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { assert instance != null && methodName != null && parameters != null; return invokeMethod(instance, methodName, createParameterTypes(parameters), parameters); } public static Object invokeMethod(final Object instance, final String methodName, final Class<?>[] parTypes, final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { assert instance != null && methodName != null && parTypes != null && parameters != null; final Method method = getMethod(instance.getClass(), methodName, parTypes, parameters); method.setAccessible(true); return method.invoke(instance, parameters); } private static Class<?>[] createParameterTypes(final Object[] parameters) { assert parameters != null; final List<Class<?>> types = new ArrayList<Class<?>>(); for (final Object object : parameters) { types.add(object.getClass()); } return types.toArray(new Class[types.size()]); } private static Method getMethod(final Class<? extends Object> clazz, final String methodName, final Class<?>[] parTypes, final Object[] parameters) throws NoSuchMethodException { assert clazz != null && methodName != null && parTypes != null && parameters != null; try { return clazz.getDeclaredMethod(methodName, parTypes); } catch (NoSuchMethodException e) { final Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return getMethod(superClass, methodName, parTypes, parameters); } throw e; } } }