33import static datadog .trace .bootstrap .AgentClassLoading .INJECTING_HELPERS ;
44import static java .util .Arrays .asList ;
55
6+ import datadog .trace .api .InstrumenterConfig ;
67import datadog .trace .bootstrap .instrumentation .api .EagerHelper ;
78import datadog .trace .util .JDK9ModuleAccess ;
89import java .io .IOException ;
910import java .lang .ref .WeakReference ;
1011import java .lang .reflect .AnnotatedElement ;
1112import java .security .CodeSource ;
1213import java .security .ProtectionDomain ;
14+ import java .util .Collection ;
1315import java .util .Collections ;
1416import java .util .HashSet ;
1517import java .util .LinkedHashMap ;
2224import net .bytebuddy .description .type .TypeDescription ;
2325import net .bytebuddy .dynamic .ClassFileLocator ;
2426import net .bytebuddy .dynamic .DynamicType ;
25- import net .bytebuddy .dynamic .loading .ClassInjector ;
2627import net .bytebuddy .utility .JavaModule ;
2728import org .slf4j .Logger ;
2829import org .slf4j .LoggerFactory ;
@@ -34,6 +35,9 @@ public class HelperInjector implements Instrumenter.TransformingAdvice {
3435 private static final ClassFileLocator classFileLocator =
3536 ClassFileLocator .ForClassLoader .of (Utils .getExtendedClassLoader ());
3637
38+ private static final boolean unsafeClassInjection =
39+ InstrumenterConfig .get ().isUnsafeClassInjection ();
40+
3741 private final boolean useAgentCodeSource ;
3842 private final AdviceShader adviceShader ;
3943 private final String requestingName ;
@@ -132,7 +136,7 @@ public DynamicType.Builder<?> transform(
132136 }
133137
134138 final Map <String , byte []> classnameToBytes = getHelperMap ();
135- final Map < String , Class <?>> classes = injectClassLoader (classLoader , classnameToBytes );
139+ final Collection < Class <?>> classes = injectClassLoader (classLoader , classnameToBytes );
136140
137141 // all datadog helper classes are in the unnamed module
138142 // and there's exactly one unnamed module per classloader
@@ -141,7 +145,7 @@ public DynamicType.Builder<?> transform(
141145 }
142146
143147 // forcibly initialize any eager helpers
144- for (Class <?> clazz : classes . values () ) {
148+ for (Class <?> clazz : classes ) {
145149 if (EagerHelper .class .isAssignableFrom (clazz )) {
146150 try {
147151 clazz .getMethod ("init" ).invoke (null );
@@ -173,16 +177,30 @@ public DynamicType.Builder<?> transform(
173177 return builder ;
174178 }
175179
176- private Map < String , Class <?>> injectClassLoader (
180+ private Collection < Class <?>> injectClassLoader (
177181 final ClassLoader classLoader , final Map <String , byte []> classnameToBytes ) {
178182 INJECTING_HELPERS .begin ();
179183 try {
180184 if (useAgentCodeSource ) {
181185 ProtectionDomain protectionDomain = createProtectionDomain (classLoader );
182- return new ClassInjector .UsingReflection (classLoader , protectionDomain )
183- .injectRaw (classnameToBytes );
186+ if (unsafeClassInjection ) {
187+ return new net .bytebuddy .dynamic .loading .ClassInjector .UsingReflection (
188+ classLoader , protectionDomain )
189+ .injectRaw (classnameToBytes )
190+ .values ();
191+ } else {
192+ return datadog .instrument .classinject .ClassInjector .injectClasses (
193+ classnameToBytes , protectionDomain );
194+ }
184195 } else {
185- return new ClassInjector .UsingReflection (classLoader ).injectRaw (classnameToBytes );
196+ if (unsafeClassInjection ) {
197+ return new net .bytebuddy .dynamic .loading .ClassInjector .UsingReflection (classLoader )
198+ .injectRaw (classnameToBytes )
199+ .values ();
200+ } else {
201+ return datadog .instrument .classinject .ClassInjector .injectClasses (
202+ classnameToBytes , classLoader );
203+ }
186204 }
187205 } finally {
188206 INJECTING_HELPERS .end ();
0 commit comments