Source:
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("close") && args.length == 0) { log.debug("Closing leadership proxy"); isClosed = true; executor.shutdownNow(); clearDelegate(); return null; } final LeadershipToken leadershipToken = getLeadershipToken(); Object maybeValidDelegate = delegateRef.get(); ListenableFuture<StillLeadingStatus> leadingFuture = Tracers.wrapListenableFuture("validate-leadership", () -> statusRetrier.execute(() -> leaderElectionService.isStillLeading(leadershipToken))); ListenableFuture<Object> delegateFuture = Futures.transform(leadingFuture, leading -> { if (leading == StillLeadingStatus.NOT_LEADING || leading == StillLeadingStatus.NO_QUORUM) { markAsNotLeading(leadershipToken, null ); } if (isClosed) { throw new IllegalStateException("already closed proxy for " + interfaceClass.getName()); } Preconditions.checkNotNull(maybeValidDelegate, "%s backing is null", interfaceClass.getName()); return maybeValidDelegate; }, executionExecutor); if (!method.getReturnType().equals(ListenableFuture.class)) { Object delegate = AtlasFutures.getUnchecked(delegateFuture); try (CloseableTracer ignored = CloseableTracer.startSpan("execute-on-delegate")) { return method.invoke(delegate, args); } catch (InvocationTargetException e) { throw handleDelegateThrewException(leadershipToken, e); } } else { return FluentFuture.from(delegateFuture) .transformAsync(delegate -> Tracers.wrapListenableFuture("execute-on-delegate-async", () -> { try { return (ListenableFuture<Object>) method.invoke(delegate, args); } catch (IllegalAccessException | InvocationTargetException e) { return Futures.immediateFailedFuture(e); } }), executionExecutor) .catchingAsync(InvocationTargetException.class, e -> { throw handleDelegateThrewException(leadershipToken, e); }, executionExecutor); } }

Target:
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("close") && args.length == 0) { log.debug("Closing leadership proxy"); isClosed = true; executor.shutdownNow(); clearDelegate(); return null; } final LeadershipToken leadershipToken = getLeadershipToken(); Object maybeValidDelegate = delegateRef.get(); ListenableFuture<StillLeadingStatus> leadingFuture = Tracers.wrapListenableFuture("validate-leadership", () -> statusRetrier.execute( () -> Tracers.wrapListenableFuture("validate-leadership-attempt", () -> leaderElectionService.isStillLeading(leadershipToken)))); ListenableFuture<Object> delegateFuture = Futures.transform(leadingFuture, leading -> { if (leading == StillLeadingStatus.NOT_LEADING || leading == StillLeadingStatus.NO_QUORUM) { markAsNotLeading(leadershipToken, null ); } if (isClosed) { throw new IllegalStateException("already closed proxy for " + interfaceClass.getName()); } Preconditions.checkNotNull(maybeValidDelegate, "%s backing is null", interfaceClass.getName()); return maybeValidDelegate; }, executionExecutor); if (!method.getReturnType().equals(ListenableFuture.class)) { Object delegate = AtlasFutures.getUnchecked(delegateFuture); try (CloseableTracer ignored = CloseableTracer.startSpan("execute-on-delegate")) { return method.invoke(delegate, args); } catch (InvocationTargetException e) { throw handleDelegateThrewException(leadershipToken, e); } } else { return FluentFuture.from(delegateFuture) .transformAsync(delegate -> Tracers.wrapListenableFuture("execute-on-delegate-async", () -> { try { return (ListenableFuture<Object>) method.invoke(delegate, args); } catch (IllegalAccessException | InvocationTargetException e) { return Futures.immediateFailedFuture(e); } }), executionExecutor) .catchingAsync(InvocationTargetException.class, e -> { throw handleDelegateThrewException(leadershipToken, e); }, executionExecutor); } }