1111import io .opentelemetry .javaagent .tooling .bytebuddy .ExceptionHandlers ;
1212import java .io .FileOutputStream ;
1313import java .io .IOException ;
14+ import java .util .function .BiFunction ;
1415import net .bytebuddy .agent .builder .AgentBuilder ;
1516import net .bytebuddy .asm .Advice ;
1617import net .bytebuddy .description .method .MethodDescription ;
@@ -65,7 +66,32 @@ public void applyAdviceToMethod(
6566
6667 private ClassFileLocator getAdviceLocator (ClassLoader classLoader ) {
6768 ClassFileLocator classFileLocator = ClassFileLocator .ForClassLoader .of (classLoader );
68- return transformAdvice ? new AdviceLocator (classFileLocator ) : classFileLocator ;
69+ return new AdviceLocator (
70+ classFileLocator ,
71+ (slashClassName , bytes ) -> {
72+ if (transformAdvice ) {
73+ // rewrite inline advice to a from accepted by indy instrumentation
74+ return transformAdvice (slashClassName , bytes );
75+ } else {
76+ // verify that advice does not call VirtualField.find
77+ // NOTE: this check is here to help converting the advice for the indy instrumentation,
78+ // it can be removed once the conversion is completed
79+ VirtualFieldChecker .check (bytes );
80+ return bytes ;
81+ }
82+ });
83+ }
84+
85+ private static byte [] transformAdvice (String slashClassName , byte [] bytes ) {
86+ byte [] result = AdviceTransformer .transform (bytes );
87+ if (result != null ) {
88+ dump (slashClassName , result );
89+ InstrumentationModuleClassLoader .bytecodeOverride .put (
90+ slashClassName .replace ('/' , '.' ), result );
91+ } else {
92+ result = bytes ;
93+ }
94+ return result ;
6995 }
7096
7197 @ Override
@@ -79,15 +105,17 @@ public AgentBuilder.Identified.Extendable getAgentBuilder() {
79105
80106 private static class AdviceLocator implements ClassFileLocator {
81107 private final ClassFileLocator delegate ;
108+ private final BiFunction <String , byte [], byte []> transform ;
82109
83- AdviceLocator (ClassFileLocator delegate ) {
110+ AdviceLocator (ClassFileLocator delegate , BiFunction < String , byte [], byte []> transform ) {
84111 this .delegate = delegate ;
112+ this .transform = transform ;
85113 }
86114
87115 @ Override
88116 public Resolution locate (String name ) throws IOException {
89117 Resolution resolution = delegate .locate (name );
90- return new AdviceTransformingResolution (name , resolution );
118+ return new AdviceTransformingResolution (name , resolution , transform );
91119 }
92120
93121 @ Override
@@ -99,10 +127,13 @@ public void close() throws IOException {
99127 private static class AdviceTransformingResolution implements Resolution {
100128 private final String name ;
101129 private final Resolution delegate ;
130+ private final BiFunction <String , byte [], byte []> transform ;
102131
103- AdviceTransformingResolution (String name , Resolution delegate ) {
132+ AdviceTransformingResolution (
133+ String name , Resolution delegate , BiFunction <String , byte [], byte []> transform ) {
104134 this .name = name ;
105135 this .delegate = delegate ;
136+ this .transform = transform ;
106137 }
107138
108139 @ Override
@@ -113,14 +144,7 @@ public boolean isResolved() {
113144 @ Override
114145 public byte [] resolve () {
115146 byte [] bytes = delegate .resolve ();
116- byte [] result = AdviceTransformer .transform (bytes );
117- if (result != null ) {
118- dump (name , result );
119- InstrumentationModuleClassLoader .bytecodeOverride .put (name .replace ('/' , '.' ), result );
120- } else {
121- result = bytes ;
122- }
123- return result ;
147+ return transform .apply (name , bytes );
124148 }
125149 }
126150
0 commit comments