ReflectionMethodInvoker { public R invoke(T obj, Object... args) throws NoSuchMethodException { Method targetMethod = getTargetMethod(); try { Object rawResult = targetMethod.invoke(obj, args); return returnType.cast(rawResult); } catch (IllegalAccessException e) { throw new SdkClientException(e); } catch (InvocationTargetException e) { throw new SdkClientException(e); } } ReflectionMethodInvoker(Class<T> clazz,
                                   Class<R> returnType,
                                   String methodName,
                                   Class<?>... parameterTypes); R invoke(T obj, Object... args); void initialize(); boolean isInitialized();  }
@Test public void invokeCanInvokeMethodAndReturnsCorrectResult() throws Exception { ReflectionMethodInvoker<String, Integer> invoker = new ReflectionMethodInvoker<String, Integer>(String.class, Integer.class, "indexOf", String.class, int.class); assertThat(invoker.invoke("ababab", "ab", 1), is(2)); }
@Test public void invokeCanReturnVoid() throws Exception { ReflectionMethodInvoker<InvokeTestClass, Void> invoker = new ReflectionMethodInvoker<InvokeTestClass, Void>(InvokeTestClass.class, Void.class, "happyVoid"); invoker.invoke(invokeTestInstance); }
@Test public void invokeThrowsNoSuchMethodExceptionWhenMethodSignatureNotFound() throws Exception { ReflectionMethodInvoker<String, Integer> invoker = new ReflectionMethodInvoker<String, Integer>(String.class, Integer.class, "foo", String.class, int.class); exception.expect(NoSuchMethodException.class); invoker.invoke("ababab", "ab", 1); }
@Test public void invoke_callThrowsNullPointerException_throwsAsSdkException() throws Exception { ReflectionMethodInvoker<String, Integer> invoker = new ReflectionMethodInvoker<String, Integer>(String.class, Integer.class, null, String.class, int.class); exception.expect(SdkClientException.class); exception.expectCause(Matchers.<NullPointerException>instanceOf(NullPointerException.class)); invoker.invoke("ababab", "ab", 1); }
@Test public void invoke_invokedMethodThrowsException_throwsAsSdkException() throws Exception { ReflectionMethodInvoker<InvokeTestClass, Void> invoker = new ReflectionMethodInvoker<InvokeTestClass, Void>(InvokeTestClass.class, Void.class, "throwsException"); exception.expect(SdkClientException.class); exception.expectCause(Matchers.<InvocationTargetException>instanceOf(InvocationTargetException.class)); invoker.invoke(invokeTestInstance); }
@Test public void invoke_invokePrivateMethod_throwsNoSuchMethodException() throws Exception { ReflectionMethodInvoker<InvokeTestClass, Void> invoker = new ReflectionMethodInvoker<InvokeTestClass, Void>(InvokeTestClass.class, Void.class, "illegalAccessException"); exception.expect(NoSuchMethodException.class); invoker.invoke(invokeTestInstance); }
@Test public void invoke_canBeCalledMultipleTimes() throws Exception { ReflectionMethodInvoker<String, Integer> invoker = new ReflectionMethodInvoker<String, Integer>(String.class, Integer.class, "indexOf", String.class, int.class); assertThat(invoker.invoke("ababab", "ab", 1), is(2)); assertThat(invoker.invoke("ababab", "ab", 1), is(2)); }