Java Reflection Method Invoke invokeMethodHandleException(Method method, Object... args)

Here you can find the source of invokeMethodHandleException(Method method, Object... args)

Description

Invokes given method with given arguments.

License

Open Source License

Exception

Parameter Description
AssertionError if error happened.

Declaration

public static Object invokeMethodHandleException(Method method, Object... args)
        throws InvocationTargetException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*  ww w .ja va  2  s  . com*/
     * Invokes given method with given arguments.
     * For instance method the 1st argument is used as {@code this}.
     * <br/>
     * Unlike {@link #invokeMethod(Method, Object...)} an exception thrown from target method is
     * not treated as error.  
     * 
     * @throws AssertionError if error happened.
     */
    public static Object invokeMethodHandleException(Method method, Object... args)
            throws InvocationTargetException {
        try {
            method.setAccessible(true);
            if (Modifier.isStatic(method.getModifiers())) {
                return method.invoke(null, args);
            } else {
                Object this_ = args[0];
                Object[] newArgs = new Object[args.length - 1];
                System.arraycopy(args, 1, newArgs, 0, args.length - 1);
                return method.invoke(this_, newArgs);
            }
        } catch (IllegalAccessException e) {
            throw new AssertionError(e);
        } catch (IllegalArgumentException e) {
            throw new AssertionError(e);
        }
    }
}

Related

  1. invokeMethodAndGet(Object object, String methodName, Object... args)
  2. invokeMethodAndGetRecursively(Object object, String methodName, Object... args)
  3. invokeMethodAndReturn(Class clazz, String method, Object object)
  4. invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)
  5. invokeMethodByName(final Object obj, final String methodName, final Object[] args)
  6. invokeMethodNoArgs(final Object obj, final String methodName, final Class... returnTypePreference)
  7. invokeMethodNoArgs(Object target, String methodName)
  8. invokeMethodOfClass(Class target, Method method)
  9. invokeMethodOnLoader(ClassLoader cl, String methodName, Object... params)