Java Method Call invoke(Class returnType, Method m, Object obj, Object... args)

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

Description

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

License

Apache License

Parameter

Parameter Description
returnType a parameter
m a parameter
obj a parameter
args a parameter
T a parameter

Exception

Parameter Description
InvocationTargetException an exception

Return

The result of the method invocation

Declaration

@SuppressWarnings("unchecked")
public static <T> T invoke(Class<T> returnType, Method m, Object obj, Object... args)
        throws InvocationTargetException 

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  w  w w . java 2  s  .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
     * @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. invoke(Class annotationClass, Object instance, Object[] parameters)
  2. invoke(Class clazz, Object obj, String methodName, Object... args)
  3. invoke(Class clazz, String methodName, Class[] parameterTypes, Object instance, Object[] args, Class returnType)
  4. invoke(Class clazz, String methodName, Object instance, Class[] signature, Object... args)
  5. invoke(Class cls, String methodName, Object... parameter)
  6. invoke(E e, String methodName)
  7. invoke(final Method m, final Object obj, final Object... args)
  8. invoke(final Method method, final Object obj, final Object... args)
  9. invoke(final Object component, final Method method, final Class type, final String value)