public static <T> T createDelegatingProxy(Class<T> clazz, final Object delegate) {
    final Class delegateClass = delegate.getClass();
    return (T)
        Proxy.newProxyInstance(
            clazz.getClassLoader(),
            new Class[] {clazz},
            (proxy, method, args) -> {
              try {
                Method delegateMethod =
                    findDelegateMethod(delegateClass, method.getName(), method.getParameterTypes());
                delegateMethod.setAccessible(true);
                return delegateMethod.invoke(delegate, args);
              } catch (NoSuchMethodException e) {
                return PRIMITIVE_RETURN_VALUES.get(method.getReturnType().getName());
              } catch (InvocationTargetException e) {
                // Required to propagate the correct throwable.
                throw e.getTargetException();
              }
            });
  }
@Test
  public void createDelegatingProxy_defersToDelegate() {
    DelegatingProxyFixture fixture =
        ReflectionHelpers.createDelegatingProxy(DelegatingProxyFixture.class, new Delegate());
    assertThat(fixture.delegateMethod()).isEqualTo("called");
  }