Java Reflection Method Invoke invoke(Method method, Object object, Object... parameters)

Here you can find the source of invoke(Method method, Object object, Object... parameters)

Description

Invoke a method on an object and return whatever it returns.

License

Open Source License

Parameter

Parameter Description
method The method to invoke.
object The instance or class (for static methods) on which to invoke the method.
parameters The parameters to pass to the method.

Exception

Parameter Description
IOException If I/O errors occur
StripesRuntimeException If anything else goes wrong

Return

Whatever the method returns.

Declaration

@SuppressWarnings("unchecked")
protected static <T> T invoke(Method method, Object object, Object... parameters)
        throws IOException, RuntimeException 

Method Source Code

//package com.java2s;

import java.io.IOException;

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

public class Main {
    /**//from  w w  w  .ja  v a2 s.c o m
     * Invoke a method on an object and return whatever it returns.
     * 
     * @param method The method to invoke.
     * @param object The instance or class (for static methods) on which to invoke the method.
     * @param parameters The parameters to pass to the method.
     * @return Whatever the method returns.
     * @throws IOException If I/O errors occur
     * @throws StripesRuntimeException If anything else goes wrong
     */
    @SuppressWarnings("unchecked")
    protected static <T> T invoke(Method method, Object object, Object... parameters)
            throws IOException, RuntimeException {
        try {
            return (T) method.invoke(object, parameters);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof IOException)
                throw (IOException) e.getTargetException();
            else
                throw new RuntimeException(e);
        }
    }
}

Related

  1. invoke(Method method, Object obj, Object... args)
  2. invoke(Method method, Object obj, Object... args)
  3. invoke(Method method, Object object, Object... args)
  4. invoke(Method method, Object object, Object... arguments)
  5. invoke(Method method, Object object, Object... arguments)
  6. invoke(Method method, Object object, Object[] args, Class exceptionToThrow)
  7. invoke(Method method, Object target, Object... args)
  8. invoke(Method method, Object target, Object... arguments)
  9. invoke(Method method, T instance, Object... params)