MethodCoercions { public static Predicate<Method> matchSingleParameterMethod(final String methodName, final Object argument) { checkNotNull(methodName, "methodName"); checkNotNull(argument, "argument"); return new Predicate<Method>() { @Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; Type[] parameterTypes = input.getGenericParameterTypes(); return parameterTypes.length == 1 && TypeCoercions.tryCoerce(argument, TypeToken.of(parameterTypes[0])).isPresentAndNonNull(); } }; }  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 Predicate<Method> matchMethodByName(final String methodName); static Predicate<Method> matchMultiParameterMethod(final List<?> arguments); static Maybe<?> tryFindAndInvokeMultiParameterMethod(Object instanceOrClazz, Iterable<Method> methods, List<?> arguments); static Maybe<?> tryFindAndInvokeMultiParameterMethod(Object instance, String methodName, List<?> arguments); static Maybe<?> tryFindAndInvokeBestMatchingMethod(Object instance, String methodName, Object argument);  }
@Test public void testMatchSingleParameterMethod() throws Exception { Predicate<Method> predicate = MethodCoercions.matchSingleParameterMethod("singleParameterMethod", "42"); assertTrue(predicate.apply(singleParameterMethod)); assertFalse(predicate.apply(multiParameterMethod)); assertFalse(predicate.apply(singleCollectionParameterMethod)); }
@Test public void testMatchSingleCollectionParameterMethod() throws Exception { Predicate<Method> predicate = MethodCoercions.matchSingleParameterMethod("singleCollectionParameterMethod", ImmutableList.of("42")); assertFalse(predicate.apply(singleParameterMethod)); assertFalse(predicate.apply(multiParameterMethod)); assertTrue(predicate.apply(singleCollectionParameterMethod)); }