Skip to content

Commit 96f0356

Browse files
committed
Replace reflection with MethodHandles usage in indy bootstraping code to avoid recursion
1 parent ad7440f commit 96f0356

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/indy/InstrumentationModuleClassLoader.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,15 @@ public MethodHandles.Lookup getLookup() {
109109
if (cachedLookup == null) {
110110
// Load the injected copy of LookupExposer and invoke it
111111
try {
112+
MethodType getLookupType = MethodType.methodType(MethodHandles.Lookup.class);
112113
// we don't mind the race condition causing the initialization to run multiple times here
113114
Class<?> lookupExposer = loadClass(LookupExposer.class.getName());
114-
cachedLookup = (MethodHandles.Lookup) lookupExposer.getMethod("getLookup").invoke(null);
115-
} catch (Exception e) {
115+
// Note: we must use MethodHandles instead of reflection here to avoid a recursion
116+
// for our internal ReflectionInstrumentationModule which instruments reflection methods
117+
cachedLookup = (MethodHandles.Lookup) MethodHandles.publicLookup()
118+
.findStatic(lookupExposer, "getLookup", getLookupType)
119+
.invoke();
120+
} catch (Throwable e) {
116121
throw new IllegalStateException(e);
117122
}
118123
}

0 commit comments

Comments
 (0)