@Test public void testMatchMultiParameterMethod() throws Exception { Predicate<Method> predicate = MethodCoercions.matchMultiParameterMethod("multiParameterMethod", ImmutableList.of("true", "42")); assertFalse(predicate.apply(singleParameterMethod)); assertTrue(predicate.apply(multiParameterMethod)); assertFalse(predicate.apply(singleCollectionParameterMethod)); }
public static Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments) { checkNotNull(methodName, "methodName"); checkNotNull(arguments, "arguments"); return new Predicate<Method>() { @Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; int numOptionParams = arguments.size(); Type[] parameterTypes = input.getGenericParameterTypes(); if (parameterTypes.length != numOptionParams) return false; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { if (!TypeCoercions.tryCoerce(((List<?>) arguments).get(paramCount), TypeToken.of(parameterTypes[paramCount])).isPresentAndNonNull()) return false; } return true; } }; }
MethodCoercions { public static Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments) { checkNotNull(methodName, "methodName"); checkNotNull(arguments, "arguments"); return new Predicate<Method>() { @Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; int numOptionParams = arguments.size(); Type[] parameterTypes = input.getGenericParameterTypes(); if (parameterTypes.length != numOptionParams) return false; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { if (!TypeCoercions.tryCoerce(((List<?>) arguments).get(paramCount), TypeToken.of(parameterTypes[paramCount])).isPresentAndNonNull()) return false; } return true; } }; } }
MethodCoercions { public static Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments) { checkNotNull(methodName, "methodName"); checkNotNull(arguments, "arguments"); return new Predicate<Method>() { @Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; int numOptionParams = arguments.size(); Type[] parameterTypes = input.getGenericParameterTypes(); if (parameterTypes.length != numOptionParams) return false; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { if (!TypeCoercions.tryCoerce(((List<?>) arguments).get(paramCount), TypeToken.of(parameterTypes[paramCount])).isPresentAndNonNull()) return false; } return true; } }; }  }
MethodCoercions { public static Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments) { checkNotNull(methodName, "methodName"); checkNotNull(arguments, "arguments"); return new Predicate<Method>() { @Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; int numOptionParams = arguments.size(); Type[] parameterTypes = input.getGenericParameterTypes(); if (parameterTypes.length != numOptionParams) return false; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { if (!TypeCoercions.tryCoerce(((List<?>) arguments).get(paramCount), TypeToken.of(parameterTypes[paramCount])).isPresentAndNonNull()) return false; } return true; } }; }  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 Predicate<Method> matchMultiParameterMethod(final String methodName, final List<?> arguments) { checkNotNull(methodName, "methodName"); checkNotNull(arguments, "arguments"); return new Predicate<Method>() { @Override public boolean apply(@Nullable Method input) { if (input == null) return false; if (!input.getName().equals(methodName)) return false; int numOptionParams = arguments.size(); Type[] parameterTypes = input.getGenericParameterTypes(); if (parameterTypes.length != numOptionParams) return false; for (int paramCount = 0; paramCount < numOptionParams; paramCount++) { if (!TypeCoercions.tryCoerce(((List<?>) arguments).get(paramCount), TypeToken.of(parameterTypes[paramCount])).isPresentAndNonNull()) return false; } return true; } }; }  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);  }