Here you can find the source of invokeMethod(final Object object, final String methodName, final Class>[] paramTypes, final Object[] parameters)
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] paramTypes, final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Gerold Klinger and sourceheads Information Technology GmbH. * All rights reserved.//w w w . ja v a 2 s .co m * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Contributors: * ... ******************************************************************************/ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; public class Main { public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] paramTypes, final Object[] parameters) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { final Method method = getDeclaredMethod(object, methodName, paramTypes); method.setAccessible(true); return method.invoke(object, parameters); } public static Method getDeclaredMethod(final Object object, final String name, final Class<?>... paramTypes) throws NoSuchMethodException { Method method = null; Class<?> clazz = object.getClass(); // TODO: this is a hack, implement better solution do { try { method = clazz.getDeclaredMethod(name, paramTypes); } catch (Exception ignored) { } } while (method == null && (clazz = clazz.getSuperclass()) != null); if (method == null) { throw new NoSuchMethodException( object.getClass() + "." + name + '(' + Arrays.toString(paramTypes) + ')'); } return method; } }