Java Method Call invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs)

Here you can find the source of invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs)

Description

invoke Virtual

License

Apache License

Declaration

public static <T> T invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Method Source Code


//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");
    }
}

Related

  1. invokeTargetMethods(Object obj, T data, List methods)
  2. invokeToStringMethod(Object value, Class type)
  3. invokeUnchecked(Method method, Object target, Object... arguments)
  4. invokeUnwrapException(final Object target, final Method method, @Nullable final Object[] args)
  5. invokeValue(AnnotatedElement element, Class annotationClass)
  6. invokeVirtual(T o, Method method, Object... pa)
  7. invokeVoid(MethodHandle mh)
  8. invokeVoidNoArgMethod(Class declaringClass, String methodName, Object instance)
  9. invokeVoidSafe(Object obj, String methodName, Class paramClass, Object param)