|  | 
| 6 | 6 | package io.opentelemetry.javaagent.tooling.util; | 
| 7 | 7 | 
 | 
| 8 | 8 | import io.opentelemetry.instrumentation.api.internal.cache.Cache; | 
|  | 9 | +import io.opentelemetry.javaagent.tooling.HelperInjector; | 
| 9 | 10 | import java.lang.ref.WeakReference; | 
| 10 | 11 | import java.lang.reflect.Field; | 
| 11 | 12 | import java.util.Collections; | 
|  | 
| 15 | 16 | import net.bytebuddy.ByteBuddy; | 
| 16 | 17 | import net.bytebuddy.description.modifier.Ownership; | 
| 17 | 18 | import net.bytebuddy.description.modifier.Visibility; | 
| 18 |  | -import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; | 
| 19 | 19 | 
 | 
| 20 | 20 | class ClassLoaderMap { | 
| 21 | 21 |   private static final Cache<ClassLoader, WeakReference<Map<Object, Object>>> data = Cache.weak(); | 
| @@ -51,20 +51,24 @@ private static Map<Object, Object> getClassLoaderData( | 
| 51 | 51 | 
 | 
| 52 | 52 |   @SuppressWarnings("unchecked") | 
| 53 | 53 |   private static Map<Object, Object> createMap(ClassLoader classLoader) { | 
|  | 54 | +    String className = | 
|  | 55 | +        "io.opentelemetry.javaagent.ClassLoaderData$$" | 
|  | 56 | +            + Integer.toHexString(System.identityHashCode(classLoader)); | 
| 54 | 57 |     // generate a class with a single static field named "data" and define it in the given class | 
| 55 | 58 |     // loader | 
| 56 |  | -    Class<?> clazz = | 
|  | 59 | +    byte[] bytes = | 
| 57 | 60 |         new ByteBuddy() | 
| 58 | 61 |             .subclass(Object.class) | 
| 59 |  | -            .name( | 
| 60 |  | -                "io.opentelemetry.javaagent.ClassLoaderData$$" | 
| 61 |  | -                    + Integer.toHexString(System.identityHashCode(classLoader))) | 
|  | 62 | +            .name(className) | 
| 62 | 63 |             .defineField("data", Object.class, Ownership.STATIC, Visibility.PUBLIC) | 
| 63 | 64 |             .make() | 
| 64 |  | -            .load(classLoader, ClassLoadingStrategy.Default.INJECTION.allowExistingTypes()) | 
| 65 |  | -            .getLoaded(); | 
|  | 65 | +            .getBytes(); | 
|  | 66 | +    HelperInjector.injectHelperClasses( | 
|  | 67 | +        classLoader, Collections.singletonMap(className, () -> bytes)); | 
| 66 | 68 |     Map<Object, Object> map; | 
| 67 | 69 |     try { | 
|  | 70 | +      Class<?> clazz = Class.forName(className, false, classLoader); | 
|  | 71 | + | 
| 68 | 72 |       Field field = clazz.getField("data"); | 
| 69 | 73 |       synchronized (classLoader) { | 
| 70 | 74 |         map = (Map<Object, Object>) field.get(classLoader); | 
|  | 
0 commit comments