Here you can find the source of invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs)
public static <T> T invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
//package com.java2s; //License from project: Apache License import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; public class Main { public static <T> T invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Class clazz = onObj.getClass(); while (clazz != null) { final Method method = clazz.getDeclaredMethod(methodName, declaredArgTypes); if (method != null) { method.setAccessible(true); return (T) method.invoke(onObj, withArgs); }//from w w w .j a va2s. c o m clazz = clazz.getSuperclass(); } throw new NoSuchMethodException("No method found called " + methodName + " accomodating arguments " + Arrays.toString(declaredArgTypes) + " on " + onObj.getClass() + " or its base classes"); } }