content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.finatra;
  
  import static io.opentelemetry.javaagent.instrumentation.finatra.FinatraSingletons.setCallbackClass;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.returns;
  
  import com.twitter.finatra.http.internal.routing.Route;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  import scala.Function1;
  
  public class FinatraRouteBuilderInstrumentation implements TypeInstrumentation {
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("com.twitter.finatra.http.RouteBuilder");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          named("build").and(returns(named("com.twitter.finatra.http.internal.routing.Route"))),
          this.getClass().getName() + "$BuildAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class BuildAdvice {
  
      @Advice.OnMethodExit(suppress = Throwable.class)
      public static void onExit(
          @Advice.Return Route route, @Advice.FieldValue("callback") Function1<?, ?> callback) {
        setCallbackClass(route, callback.getClass());
      }
    }
  }
  