Java Reflection Method Invoke invokeMethod(Method method, Object object, Object... args)

Here you can find the source of invokeMethod(Method method, Object object, Object... args)

Description

invoke Method

License

Apache License

Declaration

public static Object invokeMethod(Method method, Object object, Object... args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.List;

public class Main {

    public static Object invokeMethod(Method method, Object object, Object... args)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        if (args == null || args.length == 0) {
            return method.invoke(object);
        }/*from   www  . ja  va  2 s.  com*/

        List<Object> invokeArgs = new ArrayList<>();

        for (Class<?> typeClass : method.getParameterTypes()) {
            for (Object arg : args) {
                if (arg == null) {
                    invokeArgs.add(null);
                    break;
                } else if (typeClass.isAssignableFrom(arg.getClass())) {
                    invokeArgs.add(arg);
                    break;
                }
            }
        }

        return invokeArgs.isEmpty() ? method.invoke(object) : method.invoke(object, invokeArgs.toArray());
    }
}

Related

  1. invokeMethod(Method meth, String str)
  2. invokeMethod(Method method, Class beanClass, Object element)
  3. invokeMethod(Method method, Object bean, Object[] values)
  4. invokeMethod(Method method, Object instance, Object... parameters)
  5. invokeMethod(Method method, Object object)
  6. invokeMethod(Method method, Object object, Object... arguments)
  7. invokeMethod(Method method, Object target)
  8. invokeMethod(Method method, Object target)
  9. invokeMethod(Method method, Object target)