Here you can find the source of invokeMethod(Method method, Object object, Object... args)
public static Object invokeMethod(Method method, Object object, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
//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()); } }