content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.finaglehttp.v23_11;
  
  import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
  
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  import scala.Function1;
  
  public class PromiseMonitoredInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("com.twitter.util.Promise$Monitored");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          isConstructor().and(takesArgument(1, named("scala.Function1"))),
          this.getClass().getName() + "$WrapFunctionAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class WrapFunctionAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      @Advice.AssignReturned.ToArguments(@ToArgument(1))
      public static Function1<?, ?> wrap(@Advice.Argument(1) Function1<?, ?> function1) {
        if (function1 == null) {
          return null;
        }
  
        return Function1Wrapper.wrap(function1);
      }
    }
  }
  