JavaMethodInvoker { public Object invokeMethod(Object instance, Method method, boolean makeAccessible, Object... args) { try { if (makeAccessible) { method.setAccessible(true); } if (instance instanceof Proxy) { InvocationHandler handler = Proxy.getInvocationHandler(instance); return handler.invoke(instance, method, args); } else { return method.invoke(instance, args); } } catch (Throwable exception) { logger.error("", exception); throw new JavaFunctionInvocationException(exception, instance, method); } }  Object invokeMethod(Object instance, Method method, boolean makeAccessible, Object... args);  }
@Test public void shouldInvokePublicJavaMethod() throws NoSuchMethodException { JavaMethodInvoker invoker = new JavaMethodInvoker(); FunctionWrapper wrapper = new FunctionWrapper(); invoker.invokeMethod(wrapper, FunctionWrapper.class.getDeclaredMethod("publicMethod"), false); assertThat(wrapper.publicInvoked).isTrue(); }
@Test public void shouldInvokeProtectedJavaMethod() throws NoSuchMethodException { JavaMethodInvoker invoker = new JavaMethodInvoker(); FunctionWrapper wrapper = new FunctionWrapper(); invoker.invokeMethod(wrapper, FunctionWrapper.class.getDeclaredMethod("protectedMethod"), true); assertThat(wrapper.protectedInvoked).isTrue(); }
@Test public void shouldWrapAnyExceptionComingFromFunctionInJavaFunctionInvocationException() throws NoSuchMethodException { JavaMethodInvoker invoker = new JavaMethodInvoker(); FunctionWrapper wrapper = new FunctionWrapper(); catchException(invoker).invokeMethod(wrapper, FunctionWrapper.class.getDeclaredMethod("throwingMethod"), true); assertThat(caughtException()).isInstanceOf(JavaFunctionInvocationException.class); }