AsyncMethodInvocation class abstract interface

The MethodInvocation of a method that returns a Future (or FutureOr), with a way to run code when that future completes.

An interceptor that wants to act on the outcome of an asynchronous call (log the value, count failures) cannot just wrap proceed(): the call returns at once with a future. It needs chain, which attaches the callbacks and still returns a future of the method's own type, so the generated proxy can return it as Future<T>. Write your own MethodInterceptor with it; aspects do not need it, since @After, @AfterReturning and @AfterThrowing already wait for the future.

Spring has no equivalent: it advises the call that creates a CompletableFuture, not its completion.

// Logs the value of every async call once it is there.
class AsyncLoggingInterceptor implements MethodInterceptor {
  @override
  Object? invoke(MethodInvocation invocation) {
    final result = invocation.proceed();
    if (result is Future && invocation is AsyncMethodInvocation) {
      return invocation.chain(result, onValue: (v) => log.info('got $v'));
    }
    return result;
  }
}

Spring (Java):

Object result = invocation.proceed();
if (result instanceof CompletableFuture<?> future) {
  return future.thenApply(v -> { log.info("got {}", v); return v; });
}
return result;
Implemented types

Properties

args → List<Object?>
The arguments the next step receives; an interceptor may change them in place before calling proceed.
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
proxy → Object
The proxy the caller called, the object other beans hold. For a generated subclass proxy it is the bean itself, like target.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
signature → MethodSignature
The called member: its name, types and annotations.
no setterinherited
target → Object
The bean whose member runs: the object behind the proxy.
no setterinherited

Methods

chain(Future<Object?> future, {FutureOr<void> onValue(Object? value)?, FutureOr<void> onError(Object error)?, FutureOr<void> always()?}) → Future<Object?>
Returns a future that completes like future, after the callbacks.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
proceed([List<Object?>? args]) → Object?
Runs the next advice, or the member itself after the last one, and returns its result.
inherited
toShortString() → String
A short description for logs, such as execution(Greeter.greet(..)).
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited