@Test public void testInvokeMethodByArglist3() throws Exception { ReflectionTestFunctionLibrary library = new ReflectionTestFunctionLibrary(); List<Object> argList = new ArrayList<Object>(); argList.add("foo"); argList.add(1); argList.add(2L); ReflectionUtil.invokeMethod(library, "m", argList); assertCalled(library, "m3"); }
public static Object invokeMethod(Object object, String methodName, List<?> argList) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = findMethod(object, methodName, argList); if (method == null) { throw new NoSuchMethodException("No method "+methodName+" for arguments "+debugDumpArgList(argList)+" in "+object); } Object[] args = argList.toArray(); if (method.isVarArgs()) { Class<?> parameterTypeClass = method.getParameterTypes()[0]; Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length); for (int i = 0; i < args.length; i++) { varArgs[i] = args[i]; } args = new Object[] { varArgs }; } try { return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage()+" for arguments "+debugDumpArgList(argList)+" in "+object, e); } }
ReflectionUtil { public static Object invokeMethod(Object object, String methodName, List<?> argList) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = findMethod(object, methodName, argList); if (method == null) { throw new NoSuchMethodException("No method "+methodName+" for arguments "+debugDumpArgList(argList)+" in "+object); } Object[] args = argList.toArray(); if (method.isVarArgs()) { Class<?> parameterTypeClass = method.getParameterTypes()[0]; Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length); for (int i = 0; i < args.length; i++) { varArgs[i] = args[i]; } args = new Object[] { varArgs }; } try { return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage()+" for arguments "+debugDumpArgList(argList)+" in "+object, e); } } }
ReflectionUtil { public static Object invokeMethod(Object object, String methodName, List<?> argList) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = findMethod(object, methodName, argList); if (method == null) { throw new NoSuchMethodException("No method "+methodName+" for arguments "+debugDumpArgList(argList)+" in "+object); } Object[] args = argList.toArray(); if (method.isVarArgs()) { Class<?> parameterTypeClass = method.getParameterTypes()[0]; Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length); for (int i = 0; i < args.length; i++) { varArgs[i] = args[i]; } args = new Object[] { varArgs }; } try { return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage()+" for arguments "+debugDumpArgList(argList)+" in "+object, e); } }  }
ReflectionUtil { public static Object invokeMethod(Object object, String methodName, List<?> argList) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = findMethod(object, methodName, argList); if (method == null) { throw new NoSuchMethodException("No method "+methodName+" for arguments "+debugDumpArgList(argList)+" in "+object); } Object[] args = argList.toArray(); if (method.isVarArgs()) { Class<?> parameterTypeClass = method.getParameterTypes()[0]; Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length); for (int i = 0; i < args.length; i++) { varArgs[i] = args[i]; } args = new Object[] { varArgs }; } try { return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage()+" for arguments "+debugDumpArgList(argList)+" in "+object, e); } }  static T getJavaProperty(Object object, String propertyName, Class<T> propetyClass); static boolean hasJavaProperty(Object object, String propertyName); static Method findGetter(Object object, String propertyName); static Method findMethod(Object object, String methodName, int arity); static Method findMethod(Object object, String methodName, List<?> argList); static Method findVarArgsMethod(Object object, String methodName); static Object invokeMethod(Object object, String methodName, List<?> argList); static String debugDumpArgList(List<?> argList); }
ReflectionUtil { public static Object invokeMethod(Object object, String methodName, List<?> argList) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = findMethod(object, methodName, argList); if (method == null) { throw new NoSuchMethodException("No method "+methodName+" for arguments "+debugDumpArgList(argList)+" in "+object); } Object[] args = argList.toArray(); if (method.isVarArgs()) { Class<?> parameterTypeClass = method.getParameterTypes()[0]; Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length); for (int i = 0; i < args.length; i++) { varArgs[i] = args[i]; } args = new Object[] { varArgs }; } try { return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage()+" for arguments "+debugDumpArgList(argList)+" in "+object, e); } }  static T getJavaProperty(Object object, String propertyName, Class<T> propetyClass); static boolean hasJavaProperty(Object object, String propertyName); static Method findGetter(Object object, String propertyName); static Method findMethod(Object object, String methodName, int arity); static Method findMethod(Object object, String methodName, List<?> argList); static Method findVarArgsMethod(Object object, String methodName); static Object invokeMethod(Object object, String methodName, List<?> argList); static String debugDumpArgList(List<?> argList);  }