/** searches for the given method on the given clazz or instance, doing reasonably good matching on args etc */ public static Maybe<Method> getMethodFromArgs(Object clazzOrInstance, String method, List<?> args) { return getMethodFromArgs(clazzOrInstance, method, args, Optional.<TypeCoercer>absent()); }
/** as {@link #invokeMethodFromArgs(Object, String, List)} but giving control over whether to set it accessible */ public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible, Optional<? extends TypeCoercer> coercer) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Maybe<Method> maybeMethod = getMethodFromArgs(clazzOrInstance, method, args); if (coercer.isPresent() && maybeMethod.isAbsent()) { maybeMethod = getMethodFromArgs(clazzOrInstance, method, args, coercer); } if (maybeMethod.isAbsent()) { return Maybe.absent(Maybe.getException(maybeMethod)); } Method m = maybeMethod.get(); return Maybe.of(invokeMethodFromArgs(clazzOrInstance, m, args, setAccessible, coercer)); }
@Test public void testGetMethod() throws Exception { Method m1Short = CI1.class.getMethod("m1", String.class, int.class); Method m1Long = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(Reflections.getMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList("hello", 3)).get(), m1Short); Assert.assertEquals(Reflections.getMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList("hello", 3, 4, 5)).get(), m1Long); }