Java Method Call invokeVirtual(T o, Method method, Object... pa)

Here you can find the source of invokeVirtual(T o, Method method, Object... pa)

Description

Invoke a virtual Method method .

License

Apache License

Parameter

Parameter Description
o the instance on which the method will be invoked
method the method
pa the arguments
T generic type of the instance
R generic type of the result

Return

result of method invocation

Declaration

public static <T, R> R invokeVirtual(T o, Method method, Object... pa) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**/*w  ww . j a  v a2s.  c  o m*/
     * 
     * Invoke a virtual {@link Method method}. This method will convert all checked exception
     * to corresponding runtime exception
     * @param o the instance on which the method will be invoked
     * @param method the method
     * @param pa the arguments
     * @param <T> generic type of the instance
     * @param <R> generic type of the result
     * @return result of method invocation
     */
    public static <T, R> R invokeVirtual(T o, Method method, Object... pa) {
        try {
            return (R) method.invoke(o, pa);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new RuntimeException();
        }
    }
}

Related

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