content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v2_0;
  
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.returns;
  import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;
  
  import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import java.util.concurrent.CompletableFuture;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.asm.Advice.AssignReturned;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  public class NettyResponseFutureInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("org.asynchttpclient.netty.NettyResponseFuture");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          named("toCompletableFuture").and(takesNoArguments()).and(returns(CompletableFuture.class)),
          this.getClass().getName() + "$WrapFutureAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class WrapFutureAdvice {
  
      @AssignReturned.ToReturned
      @Advice.OnMethodExit(suppress = Throwable.class)
      public static CompletableFuture<?> onExit(@Advice.Return CompletableFuture<?> result) {
        return CompletableFutureWrapper.wrap(result, Java8BytecodeBridge.currentContext());
      }
    }
  }
  