Java Reflection Method Invoke invoke(Method method, Object object, Object[] args, Class exceptionToThrow)

Here you can find the source of invoke(Method method, Object object, Object[] args, Class exceptionToThrow)

Description

invoke

License

Open Source License

Declaration

public static <T extends Exception> Object invoke(Method method,
            Object object, Object[] args, Class<T> exceptionToThrow)
            throws T 

Method Source Code

//package com.java2s;
//   it under the terms of the GNU Lesser General Public License as published by

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

public class Main {
    public static <T extends Exception> Object invoke(Method method,
            Object object, Object[] args, Class<T> exceptionToThrow)
            throws T {
        try {/*from  ww w.ja v a 2 s. c om*/
            return method.invoke(object, args);
        } catch (IllegalAccessException e) {
            throw handleException(exceptionToThrow, e);
        } catch (InvocationTargetException e) {
            throw handleException(exceptionToThrow, e);
        }

    }

    private static <T extends Throwable> T handleException(
            Class<T> exceptionToThrow, Throwable e) {
        try {
            return exceptionToThrow.getConstructor(Throwable.class)
                    .newInstance(e);
        } catch (InstantiationException e1) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e1) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e1) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e1) {
            throw new RuntimeException(e);
        }

    }
}

Related

  1. invoke(Method method, Object obj, Object... args)
  2. invoke(Method method, Object object, Object... args)
  3. invoke(Method method, Object object, Object... arguments)
  4. invoke(Method method, Object object, Object... arguments)
  5. invoke(Method method, Object object, Object... parameters)
  6. invoke(Method method, Object target, Object... args)
  7. invoke(Method method, Object target, Object... arguments)
  8. invoke(Method method, T instance, Object... params)
  9. invoke(MethodHandle methodHandle, Object... params)