Skip to content

Commit

Permalink
Improve muzzle check for constructors (#4591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Nov 5, 2021
1 parent 123de52 commit 59c1ce1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,21 @@ private static FieldDescription.InDefinedShape findField(

private static MethodDescription.InDefinedShape findMethod(
MethodRef methodRef, TypeDescription typeOnClasspath) {

for (MethodDescription.InDefinedShape methodDescription :
typeOnClasspath.getDeclaredMethods()) {
if (methodDescription.getInternalName().equals(methodRef.getName())
&& methodDescription.getDescriptor().equals(methodRef.getDescriptor())) {
return methodDescription;
}
}

// if the method we're looking for is a constructor, we're only checking the direct super type;
// and skipping all the interfaces and indirect superclasses
if (methodRef.isConstructor()) {
return null;
}

if (typeOnClasspath.getSuperClass() != null) {
MethodDescription.InDefinedShape methodOnSupertype =
findMethod(methodRef, typeOnClasspath.getSuperClass().asErasure());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public String getDescriptor() {
return descriptor;
}

public boolean isConstructor() {
return "<init>".equals(name);
}

MethodRef merge(MethodRef anotherMethod) {
if (!equals(anotherMethod)) {
throw new IllegalStateException("illegal merge " + this + " != " + anotherMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.opentelemetry.instrumentation.test.utils.ClasspathUtils
import io.opentelemetry.javaagent.tooling.muzzle.references.ClassRef
import io.opentelemetry.javaagent.tooling.muzzle.references.Flag
import io.opentelemetry.javaagent.tooling.muzzle.references.Source
import muzzle.TestClasses
import muzzle.TestClasses.MethodBodyAdvice
import org.objectweb.asm.Type
import spock.lang.Shared
Expand Down Expand Up @@ -320,6 +321,30 @@ class ReferenceMatcherTest extends Specification {
"external helper, different field type" | "${TEST_EXTERNAL_INSTRUMENTATION_PACKAGE}.Helper" | "field" | "Lcom/external/DifferentType;"
}

def "should fail #desc helper class when the library parent class has different constructor"() {
given:
def helper = ClassRef.builder(className)
.setSuperClassName(TestClasses.BaseClassWithConstructor.name)
.build()
// muzzle codegen plugin has captured a no-arg constructor reference;
// the actual constructor of the base class on the classpath requires a long
def baseClassRef = ClassRef.builder(TestClasses.BaseClassWithConstructor.name)
.addMethod(new Source[0], new Flag[0], "<init>", Type.VOID_TYPE)
.build()

when:
def mismatches = createMatcher([(helper.className): helper, (baseClassRef.className): baseClassRef], [helper.className])
.getMismatchedReferenceSources(this.class.classLoader)

then:
getMismatchClassSet(mismatches) == [Mismatch.MissingMethod] as Set

where:
desc | className
"internal" | "io.opentelemetry.instrumentation.Helper"
"external" | "${TEST_EXTERNAL_INSTRUMENTATION_PACKAGE}.Helper"
}

private static ReferenceMatcher createMatcher(Map<String, ClassRef> references = [:],
List<String> helperClasses = []) {
new ReferenceMatcher(helperClasses, references, { it.startsWith(TEST_EXTERNAL_INSTRUMENTATION_PACKAGE) })
Expand Down
4 changes: 4 additions & 0 deletions muzzle/src/test/java/muzzle/TestClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public static class SomeClassWithFields {
public interface AnotherInterface extends SomeInterface {}
}

public abstract static class BaseClassWithConstructor {
protected BaseClassWithConstructor(long l) {}
}

public static class LdcAdvice {
public static void ldcMethod() {
MethodBodyAdvice.A.class.getName();
Expand Down

0 comments on commit 59c1ce1

Please sign in to comment.