AbstractOpenEngSBInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (handleObjectMethodsItself) { for (Method objectMethod : Object.class.getMethods()) { if (objectMethod.getName().equals(method.getName()) && Arrays.deepEquals(objectMethod.getParameterTypes(), method.getParameterTypes())) { if (Proxy.isProxyClass(proxy.getClass())) { if (Proxy.getInvocationHandler(proxy) instanceof AbstractOpenEngSBInvocationHandler) { return method.invoke(Proxy.getInvocationHandler(proxy), args); } } } } } return handleInvoke(proxy, method, args); } AbstractOpenEngSBInvocationHandler();  AbstractOpenEngSBInvocationHandler(boolean handleObjectMethodsItself); @Override Object invoke(Object proxy, Method method, Object[] args);  }
@Test public void testUseConstructurToHandleObjectMethods_shouldHandleToString() throws Throwable { Object testObject = new Object() { @Override public String toString() { return "teststring"; } }; AbstractOpenEngSBInvocationHandler abstractOpenEngSBInvocationHandler = createAbstractOpenEngSBInvocationHandlerHandlingObjectMethods(testObject); Method method = Object.class.getMethod("toString", (Class<?>[]) null); String proxyRetVal = abstractOpenEngSBInvocationHandler.invoke(testObject, method, null).toString(); assertEquals(proxyRetVal, "teststring"); }
@Test public void testUseDefaultConstructor_shouldForwardAllCalls() throws Throwable { Object testObject = new Object(); AbstractOpenEngSBInvocationHandler abstractOpenEngSBInvocationHandler = createDefaultAbstractOpenEngSBInvocationHandler(testObject); Method method = Object.class.getMethod("toString", (Class<?>[]) null); assertSame(testObject, abstractOpenEngSBInvocationHandler.invoke(testObject, method, null)); }