File tree Expand file tree Collapse file tree 3 files changed +36
-16
lines changed
instrumentation/spring/spring-webmvc
spring-webmvc-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v3_1
spring-webmvc-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v6_0
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap Expand file tree Collapse file tree 3 files changed +36
-16
lines changed Original file line number Diff line number Diff line change 1515
1616import io .opentelemetry .context .Context ;
1717import io .opentelemetry .context .Scope ;
18+ import io .opentelemetry .javaagent .bootstrap .IndyProxyHelper ;
1819import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1920import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
20- import java .lang .reflect .Field ;
2121import java .util .List ;
2222import net .bytebuddy .asm .Advice ;
2323import net .bytebuddy .description .type .TypeDescription ;
@@ -73,13 +73,7 @@ public static void afterRefresh(
7373 // inline advice: no proxy class is used
7474 filter = (OpenTelemetryHandlerMappingFilter ) bean ;
7575 } else {
76- // non-inlined advice: proxy class is used
77- try {
78- Field delegate = bean .getClass ().getField ("delegate" );
79- filter = (OpenTelemetryHandlerMappingFilter ) delegate .get (bean );
80- } catch (NoSuchFieldException | IllegalAccessException e ) {
81- throw new IllegalStateException (e );
82- }
76+ filter = IndyProxyHelper .unwrapIfNeeded (bean , OpenTelemetryHandlerMappingFilter .class );
8377 }
8478 filter .setHandlerMappings (handlerMappings );
8579 }
Original file line number Diff line number Diff line change 1515
1616import io .opentelemetry .context .Context ;
1717import io .opentelemetry .context .Scope ;
18+ import io .opentelemetry .javaagent .bootstrap .IndyProxyHelper ;
1819import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1920import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
20- import java .lang .reflect .Field ;
2121import java .util .List ;
2222import net .bytebuddy .asm .Advice ;
2323import net .bytebuddy .description .type .TypeDescription ;
@@ -74,13 +74,7 @@ public static void afterRefresh(
7474 // inline advice: no proxy class is used
7575 filter = (OpenTelemetryHandlerMappingFilter ) bean ;
7676 } else {
77- // non-inlined advice: proxy class is used
78- try {
79- Field delegate = bean .getClass ().getField ("delegate" );
80- filter = (OpenTelemetryHandlerMappingFilter ) delegate .get (bean );
81- } catch (NoSuchFieldException | IllegalAccessException e ) {
82- throw new IllegalStateException (e );
83- }
77+ filter = IndyProxyHelper .unwrapIfNeeded (bean , OpenTelemetryHandlerMappingFilter .class );
8478 }
8579 filter .setHandlerMappings (handlerMappings );
8680 }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ * SPDX-License-Identifier: Apache-2.0
4+ */
5+
6+ package io .opentelemetry .javaagent .bootstrap ;
7+
8+ import java .lang .reflect .Field ;
9+
10+ public class IndyProxyHelper {
11+
12+ private IndyProxyHelper () {
13+ }
14+
15+ public static <T > T unwrapIfNeeded (Object o , Class <T > type ) {
16+ if (type .isAssignableFrom (o .getClass ())) {
17+ return type .cast (o );
18+ }
19+ // public delegate field on indy proxy
20+ try {
21+ Field delegateField = o .getClass ().getField ("delegate" );
22+ Object delegate = delegateField .get (o );
23+ if (type .isAssignableFrom (delegate .getClass ())) {
24+ return type .cast (delegate );
25+ }
26+ } catch (NoSuchFieldException | IllegalAccessException e ) {
27+ throw new IllegalStateException (e );
28+ }
29+
30+ return null ;
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments