Skip to content

Commit

Permalink
address review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Dec 12, 2023
1 parent 5a87f6e commit 296c603
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.bytebuddy.ByteBuddy;
Expand All @@ -19,18 +20,23 @@ class ClassLoaderMap {
private static final Cache<ClassLoader, WeakReference<Map<Object, Object>>> data = Cache.weak();

public static Object get(ClassLoader classLoader, Object key) {
return getClassLoaderData(classLoader).get(key);
return getClassLoaderData(classLoader, false).get(key);
}

public static void put(ClassLoader classLoader, Object key, Object value) {
getClassLoaderData(classLoader).put(key, value);
getClassLoaderData(classLoader, true).put(key, value);
}

private static Map<Object, Object> getClassLoaderData(ClassLoader classLoader) {
private static Map<Object, Object> getClassLoaderData(
ClassLoader classLoader, boolean initialize) {
classLoader = maskNullClassLoader(classLoader);
WeakReference<Map<Object, Object>> weakReference = data.get(classLoader);
Map<Object, Object> map = weakReference != null ? weakReference.get() : null;
if (map == null) {
// skip setting up the map if get was called
if (!initialize) {
return Collections.emptyMap();
}
map = createMap(classLoader);
data.put(classLoader, new WeakReference<>(map));
}
Expand Down

0 comments on commit 296c603

Please sign in to comment.