11package datadog .trace .agent .tooling .bytebuddy ;
22
3- import datadog .trace .api .cache .DDCache ;
4- import datadog .trace .api .cache .DDCaches ;
5- import java .lang .ref .WeakReference ;
3+ import static datadog .instrument .utils .ClassLoaderIndex .getClassLoaderKeyId ;
4+
65import java .net .URL ;
76import java .util .Arrays ;
87import java .util .Objects ;
@@ -132,25 +131,17 @@ public final T get() {
132131
133132 /** Includes the classloader and class file resource it originated from. */
134133 static final class DisambiguatingTypeInfo <T > extends SharedTypeInfo <T > {
135- private static final ClassLoader BOOTSTRAP_LOADER = null ;
136- private static final LoaderId BOOTSTRAP_LOADER_ID = null ;
137-
138- private static final DDCache <ClassLoader , LoaderId > loaderIds =
139- DDCaches .newFixedSizeWeakKeyCache (64 );
140-
141- private final LoaderId loaderId ;
134+ private final int loaderId ;
142135 private final URL classFile ;
143136
144137 DisambiguatingTypeInfo (String className , T typeInfo , ClassLoader loader , URL classFile ) {
145138 super (className , typeInfo );
146- this .loaderId = loaderId (loader );
139+ this .loaderId = getClassLoaderKeyId (loader );
147140 this .classFile = classFile ;
148141 }
149142
150143 public boolean sameClassLoader (ClassLoader loader ) {
151- return BOOTSTRAP_LOADER_ID == loaderId
152- ? BOOTSTRAP_LOADER == loader
153- : loaderId .sameClassLoader (loader );
144+ return loaderId == getClassLoaderKeyId (loader );
154145 }
155146
156147 public boolean sameClassFile (URL classFile ) {
@@ -159,12 +150,6 @@ public boolean sameClassFile(URL classFile) {
159150 && sameClassFile (this .classFile , classFile );
160151 }
161152
162- private static LoaderId loaderId (ClassLoader loader ) {
163- return BOOTSTRAP_LOADER == loader
164- ? BOOTSTRAP_LOADER_ID
165- : loaderIds .computeIfAbsent (loader , LoaderId ::new );
166- }
167-
168153 /** Matches class file resources without triggering network lookups. */
169154 private static boolean sameClassFile (URL lhs , URL rhs ) {
170155 return Objects .equals (lhs .getFile (), rhs .getFile ())
@@ -173,18 +158,4 @@ private static boolean sameClassFile(URL lhs, URL rhs) {
173158 && Objects .equals (lhs .getProtocol (), rhs .getProtocol ());
174159 }
175160 }
176-
177- /** Supports classloader comparisons without strongly referencing the classloader. */
178- static final class LoaderId extends WeakReference <ClassLoader > {
179- private final int loaderHash ;
180-
181- LoaderId (ClassLoader loader ) {
182- super (loader );
183- this .loaderHash = System .identityHashCode (loader );
184- }
185-
186- boolean sameClassLoader (ClassLoader loader ) {
187- return loaderHash == System .identityHashCode (loader ) && loader == get ();
188- }
189- }
190161}
0 commit comments