@Test void testHashCode_WithWrapper() { final Runnable runnable = mock(Runnable.class); Connection wrapped = wrap(connection, Runnable.class, runnable, closeHandler); assertEquals(wrapped.hashCode(), wrapped.hashCode()); }
public static <I> Connection wrap(final Connection connection, final Class<I> wrapperInterface, final I wrapperHandler, final ConnectionCloseHandler closeHandler) { return (Connection) Proxy.newProxyInstance(wrapperHandler.getClass().getClassLoader(), new Class[]{Connection.class, wrapperInterface}, (proxy, method, args) -> { if ("equals".equals(method.getName()) && args != null && args.length == 1) { return proxy == args[0]; } else if ("hashCode".equals( method.getName()) && isEmpty(args)) { return connection.hashCode(); } else if (method.getDeclaringClass().isAssignableFrom( wrapperInterface)) { return invokeMethodAndUnwrapNestedException( method, wrapperHandler, args); } else if ("close".equals(method.getName()) && isEmpty(args)) { closeHandler.close(connection); return null; } else if ("commit".equals(method.getName()) && isEmpty(args)) { closeHandler.commit(connection); return null; } else { return invokeMethodAndUnwrapNestedException(method, connection, args); } }); }
ConnectionWrapperFactory { public static <I> Connection wrap(final Connection connection, final Class<I> wrapperInterface, final I wrapperHandler, final ConnectionCloseHandler closeHandler) { return (Connection) Proxy.newProxyInstance(wrapperHandler.getClass().getClassLoader(), new Class[]{Connection.class, wrapperInterface}, (proxy, method, args) -> { if ("equals".equals(method.getName()) && args != null && args.length == 1) { return proxy == args[0]; } else if ("hashCode".equals( method.getName()) && isEmpty(args)) { return connection.hashCode(); } else if (method.getDeclaringClass().isAssignableFrom( wrapperInterface)) { return invokeMethodAndUnwrapNestedException( method, wrapperHandler, args); } else if ("close".equals(method.getName()) && isEmpty(args)) { closeHandler.close(connection); return null; } else if ("commit".equals(method.getName()) && isEmpty(args)) { closeHandler.commit(connection); return null; } else { return invokeMethodAndUnwrapNestedException(method, connection, args); } }); } }
ConnectionWrapperFactory { public static <I> Connection wrap(final Connection connection, final Class<I> wrapperInterface, final I wrapperHandler, final ConnectionCloseHandler closeHandler) { return (Connection) Proxy.newProxyInstance(wrapperHandler.getClass().getClassLoader(), new Class[]{Connection.class, wrapperInterface}, (proxy, method, args) -> { if ("equals".equals(method.getName()) && args != null && args.length == 1) { return proxy == args[0]; } else if ("hashCode".equals( method.getName()) && isEmpty(args)) { return connection.hashCode(); } else if (method.getDeclaringClass().isAssignableFrom( wrapperInterface)) { return invokeMethodAndUnwrapNestedException( method, wrapperHandler, args); } else if ("close".equals(method.getName()) && isEmpty(args)) { closeHandler.close(connection); return null; } else if ("commit".equals(method.getName()) && isEmpty(args)) { closeHandler.commit(connection); return null; } else { return invokeMethodAndUnwrapNestedException(method, connection, args); } }); } private  ConnectionWrapperFactory(); }
ConnectionWrapperFactory { public static <I> Connection wrap(final Connection connection, final Class<I> wrapperInterface, final I wrapperHandler, final ConnectionCloseHandler closeHandler) { return (Connection) Proxy.newProxyInstance(wrapperHandler.getClass().getClassLoader(), new Class[]{Connection.class, wrapperInterface}, (proxy, method, args) -> { if ("equals".equals(method.getName()) && args != null && args.length == 1) { return proxy == args[0]; } else if ("hashCode".equals( method.getName()) && isEmpty(args)) { return connection.hashCode(); } else if (method.getDeclaringClass().isAssignableFrom( wrapperInterface)) { return invokeMethodAndUnwrapNestedException( method, wrapperHandler, args); } else if ("close".equals(method.getName()) && isEmpty(args)) { closeHandler.close(connection); return null; } else if ("commit".equals(method.getName()) && isEmpty(args)) { closeHandler.commit(connection); return null; } else { return invokeMethodAndUnwrapNestedException(method, connection, args); } }); } private  ConnectionWrapperFactory(); static Connection wrap(final Connection connection, final Class<I> wrapperInterface,
                                      final I wrapperHandler,
                                      final ConnectionCloseHandler closeHandler); static Connection wrap(final Connection connection, final ConnectionCloseHandler closeHandler); }
ConnectionWrapperFactory { public static <I> Connection wrap(final Connection connection, final Class<I> wrapperInterface, final I wrapperHandler, final ConnectionCloseHandler closeHandler) { return (Connection) Proxy.newProxyInstance(wrapperHandler.getClass().getClassLoader(), new Class[]{Connection.class, wrapperInterface}, (proxy, method, args) -> { if ("equals".equals(method.getName()) && args != null && args.length == 1) { return proxy == args[0]; } else if ("hashCode".equals( method.getName()) && isEmpty(args)) { return connection.hashCode(); } else if (method.getDeclaringClass().isAssignableFrom( wrapperInterface)) { return invokeMethodAndUnwrapNestedException( method, wrapperHandler, args); } else if ("close".equals(method.getName()) && isEmpty(args)) { closeHandler.close(connection); return null; } else if ("commit".equals(method.getName()) && isEmpty(args)) { closeHandler.commit(connection); return null; } else { return invokeMethodAndUnwrapNestedException(method, connection, args); } }); } private  ConnectionWrapperFactory(); static Connection wrap(final Connection connection, final Class<I> wrapperInterface,
                                      final I wrapperHandler,
                                      final ConnectionCloseHandler closeHandler); static Connection wrap(final Connection connection, final ConnectionCloseHandler closeHandler);  }