@Test public void testTryFindAndInvokeMultiParameterMethod() throws Exception { TestClass instance = new TestClass(); Maybe<?> maybe = MethodCoercions.tryFindAndInvokeMultiParameterMethod(instance, "multiParameterMethod", ImmutableList.of("true", "42")); assertTrue(maybe.isPresent()); assertTrue(instance.wasMultiParameterMethodCalled()); }
public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchMultiParameterMethod(methodName, arguments)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); try { int numOptionParams = ((List<?>)arguments).size(); Object[] coercedArguments = new Object[numOptionParams]; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { Object argument = arguments.get(paramCount); Type paramType = method.getGenericParameterTypes()[paramCount]; coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType)); } return Maybe.of(method.invoke(instance, coercedArguments)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } }
MethodCoercions { public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchMultiParameterMethod(methodName, arguments)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); try { int numOptionParams = ((List<?>)arguments).size(); Object[] coercedArguments = new Object[numOptionParams]; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { Object argument = arguments.get(paramCount); Type paramType = method.getGenericParameterTypes()[paramCount]; coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType)); } return Maybe.of(method.invoke(instance, coercedArguments)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } } }
MethodCoercions { public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchMultiParameterMethod(methodName, arguments)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); try { int numOptionParams = ((List<?>)arguments).size(); Object[] coercedArguments = new Object[numOptionParams]; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { Object argument = arguments.get(paramCount); Type paramType = method.getGenericParameterTypes()[paramCount]; coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType)); } return Maybe.of(method.invoke(instance, coercedArguments)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } }  }
MethodCoercions { public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchMultiParameterMethod(methodName, arguments)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); try { int numOptionParams = ((List<?>)arguments).size(); Object[] coercedArguments = new Object[numOptionParams]; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { Object argument = arguments.get(paramCount); Type paramType = method.getGenericParameterTypes()[paramCount]; coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType)); } return Maybe.of(method.invoke(instance, coercedArguments)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } }  static Predicate<Method> matchSingleParameterMethod(final String methodName, final Object argument); static Maybe<?> tryFindAndInvokeSingleParameterMethod(final Object instance, final String methodName, final Object argument); static Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments); static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments); static Maybe<?> tryFindAndInvokeBestMatchingMethod(final Object instance, final String methodName, final Object argument); }
MethodCoercions { public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments) { Class<?> clazz = instance.getClass(); Iterable<Method> methods = Arrays.asList(clazz.getMethods()); Optional<Method> matchingMethod = Iterables.tryFind(methods, matchMultiParameterMethod(methodName, arguments)); if (matchingMethod.isPresent()) { Method method = matchingMethod.get(); try { int numOptionParams = ((List<?>)arguments).size(); Object[] coercedArguments = new Object[numOptionParams]; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { Object argument = arguments.get(paramCount); Type paramType = method.getGenericParameterTypes()[paramCount]; coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType)); } return Maybe.of(method.invoke(instance, coercedArguments)); } catch (IllegalAccessException | InvocationTargetException e) { throw Exceptions.propagate(e); } } else { return Maybe.absent(); } }  static Predicate<Method> matchSingleParameterMethod(final String methodName, final Object argument); static Maybe<?> tryFindAndInvokeSingleParameterMethod(final Object instance, final String methodName, final Object argument); static Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments); static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName, final List<?> arguments); static Maybe<?> tryFindAndInvokeBestMatchingMethod(final Object instance, final String methodName, final Object argument);  }