Java Method Call invokeWithoutInvocationException(Method m, Object obj, Object... args)

Here you can find the source of invokeWithoutInvocationException(Method m, Object obj, Object... args)

Description

Invokes the given void method on the given object with the given arguments.

License

Apache License

Parameter

Parameter Description
m a parameter
obj a parameter
args a parameter

Declaration

public static void invokeWithoutInvocationException(Method m, Object obj, Object... args) 

Method Source Code

//package com.java2s;
// SMSLib is distributed under the terms of the Apache License version 2.0

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

public class Main {
    /**/*from   ww  w. j  a  v  a  2s .co m*/
     * Invokes the given method on the given object with the given arguments.
     * The result is cast to T and every kind of exception is wrapped as
     * RuntimeException
     * @param returnType 
     * @param m 
     * @param obj 
     * @param args 
     * @param <T> 
     * @return The result of the method invocation
     */
    public static <T> T invokeWithoutInvocationException(Class<T> returnType, Method m, Object obj,
            Object... args) {
        try {
            return invoke(returnType, m, obj, args);
        } catch (InvocationTargetException e) {
            // This exception is wrapped in a strange way.  The reasoning was not documented in SMS Lib code, but best not to fiddle.
            throw new RuntimeException(new RuntimeException(e.getTargetException().toString()));
        }
    }

    /**
     * Invokes the given void method on the given object with the given arguments.
     * @param m 
     * @param obj 
     * @param args 
     */
    public static void invokeWithoutInvocationException(Method m, Object obj, Object... args) {
        try {
            invoke(m, obj, args);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(new RuntimeException(e.getTargetException().toString()));
        }
    }

    /**
     * Invokes the given method on the given object with the given arguments.
     * The result is cast to T and every kind of exception is wrapped as
     * RuntimeException
     * @param returnType 
     * @param m 
     * @param obj 
     * @param args 
     * @param <T> 
     * @return The result of the method invocation
     * @throws InvocationTargetException 
     */
    @SuppressWarnings("unchecked")
    public static <T> T invoke(Class<T> returnType, Method m, Object obj, Object... args)
            throws InvocationTargetException {
        try {
            return (T) m.invoke(obj, args);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Invokes the given void method on the given object with the given arguments.
     * @param m 
     * @param obj 
     * @param args 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     */
    public static void invoke(Method m, Object obj, Object... args) throws InvocationTargetException {
        try {
            m.invoke(obj, args);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. invokeVirtual(T o, Method method, Object... pa)
  2. invokeVoid(MethodHandle mh)
  3. invokeVoidNoArgMethod(Class declaringClass, String methodName, Object instance)
  4. invokeVoidSafe(Object obj, String methodName, Class paramClass, Object param)
  5. invokeWithoutDeclaredExceptions(Method method, Object target, Object... args)