Skip to content

Commit af7ff80

Browse files
authored
Ignore java.lang.Object methods (identified by method name plus method descriptor) when creating muzzle references. (#9649)
This helps when compiling with Java 18+ because javac will now use INVOKEINTERFACE with the interface name to call java.lang.Object methods (so the 'owner' is now seen as the interface) whereas before it was using INVOKEVIRTUAL with java.lang.Object as the method 'owner'. the key change is described in https://bugs.openjdk.org/browse/JDK-8272715: "invocations of java.lang.Object methods on interfaces in the classfile will use invokeinterface referring to the interface, which is consistent with JLS 9.2. This will be done regardless of whether the interface declares the method explicitly or not."
1 parent f945e4b commit af7ff80

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/muzzle/ReferenceCreator.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import datadog.trace.bootstrap.Constants;
88
import de.thetaphi.forbiddenapis.SuppressForbidden;
99
import java.io.InputStream;
10+
import java.lang.reflect.Method;
1011
import java.util.ArrayDeque;
1112
import java.util.HashSet;
1213
import java.util.LinkedHashMap;
@@ -39,6 +40,15 @@ public class ReferenceCreator extends ClassVisitor {
3940

4041
private static final int UNDEFINED_LINE = -1;
4142

43+
/** Set containing name+descriptor signatures of Object methods. */
44+
private static final Set<String> OBJECT_METHODS = new HashSet<>();
45+
46+
static {
47+
for (Method m : Object.class.getMethods()) {
48+
OBJECT_METHODS.add(methodSig(m.getName(), Type.getMethodDescriptor(m)));
49+
}
50+
}
51+
4252
/**
4353
* Generate all references reachable from a given class.
4454
*
@@ -323,7 +333,7 @@ public void visitMethodInsn(
323333
final String name,
324334
final String descriptor,
325335
final boolean isInterface) {
326-
if (ignoreReference(owner)) {
336+
if (ignoreReference(owner) || ignoreObjectMethod(name, descriptor)) {
327337
return;
328338
}
329339

@@ -490,4 +500,12 @@ private static boolean ignoreReference(String name) {
490500
}
491501
return false;
492502
}
503+
504+
private static boolean ignoreObjectMethod(String methodName, String methodDescriptor) {
505+
return OBJECT_METHODS.contains(methodSig(methodName, methodDescriptor));
506+
}
507+
508+
private static String methodSig(String methodName, String methodDescriptor) {
509+
return methodName + methodDescriptor;
510+
}
493511
}

0 commit comments

Comments
 (0)