Java Reflection Method Invoke invokeMethods(final Object target, final List methods)

Here you can find the source of invokeMethods(final Object target, final List methods)

Description

Invoke the following list of methods (with no parameter) and return the result in a List .

License

Open Source License

Parameter

Parameter Description
target the target object.
methods the methods to invoke.

Exception

Parameter Description
InvocationTargetException if one of the underlying method throws an exception
IllegalArgumentException if the method is an instance method and the specified object argument is not aninstance of the class or interface declaring the underlying method (or of a subclassor implementor thereof); if the number of actual and formal parameters differ; if anunwrapping conversion for primitive arguments fails; or if, after possibleunwrapping, a parameter value cannot be converted to the corresponding formalparameter type by a method invocation conversion.
IllegalAccessException if one of the Method object is enforcing Java language access control and the underlyingmethod is inaccessible.

Return

the result of the method call.

Declaration

public static List<Object> invokeMethods(final Object target, final List<Method> methods)
        throws IllegalAccessException, IllegalArgumentException, 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.util.ArrayList;

import java.util.List;

public class Main {
    /**//from  w w w .ja  v  a2 s.  co m
     * Invoke the following list of methods (with no parameter) and return the result in a
     * {@link List}.
     * @param target
     *        the target object.
     * @param methods
     *        the methods to invoke.
     * @return the result of the method call.
     * @throws InvocationTargetException
     *         if one of the underlying method throws an exception
     * @throws IllegalArgumentException
     *         if the method is an instance method and the specified object argument is not an
     *         instance of the class or interface declaring the underlying method (or of a subclass
     *         or implementor thereof); if the number of actual and formal parameters differ; if an
     *         unwrapping conversion for primitive arguments fails; or if, after possible
     *         unwrapping, a parameter value cannot be converted to the corresponding formal
     *         parameter type by a method invocation conversion.
     * @throws IllegalAccessException
     *         if one of the Method object is enforcing Java language access control and the underlying
     *         method is inaccessible.
     */
    public static List<Object> invokeMethods(final Object target, final List<Method> methods)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        final List<Object> results = new ArrayList<>(methods.size());

        for (final Method method : methods) {
            results.add(method.invoke(target, (Object[]) null));
        }

        return results;
    }
}

Related

  1. invokeMethodOfClass(Class target, Method method)
  2. invokeMethodOnLoader(ClassLoader cl, String methodName, Object... params)
  3. invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
  4. invokeMethodOnObject(Object target, String methodName)
  5. invokeMethodQuietly(Object receiver, Method method, Object... args)
  6. invokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)
  7. invokeMethodWithArray2(Object target, Method method, Object[] args)
  8. invokeMethodWithKnownParamTypes(Object object, String methodName, Class[] methodParamTypes, Object... args)
  9. invokeMethodWithNoArgs(Object o, Method method)