Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@
@Slf4j
public class ByteBuddyElementMatchers {

/**
* Matches any type description that declares a super type that matches the provided matcher.
* Exceptions during matching process are logged and ignored.
*
* @param matcher The type to be checked for being a super type of the matched type.
* @param <T> The type of the matched object.
* @return A matcher that matches any type description that declares a super type that matches the
* provided matcher.
* @see ElementMatchers#hasSuperType(net.bytebuddy.matcher.ElementMatcher)
*/
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasSuperType(
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeExtendsClass(
final ElementMatcher<? super TypeDescription> matcher) {
return new SafeExtendsClassMatcher<>(new SafeErasureMatcher<>(matcher));
}

public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasInterface(
final ElementMatcher<? super TypeDescription> matcher) {
return safeHasGenericSuperType(new SafeErasureMatcher(matcher));
return new SafeHasSuperTypeMatcher<>(new SafeErasureMatcher<>(matcher), true);
}

/**
Expand All @@ -45,11 +40,11 @@ public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasSupe
* @param <T> The type of the matched object.
* @return A matcher that matches any type description that declares a super type that matches the
* provided matcher.
* @see ElementMatchers#hasGenericSuperType(net.bytebuddy.matcher.ElementMatcher)
* @see ElementMatchers#hasSuperType(net.bytebuddy.matcher.ElementMatcher)
*/
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasGenericSuperType(
final ElementMatcher<? super TypeDescription.Generic> matcher) {
return new SafeHasSuperTypeMatcher<>(matcher);
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasSuperType(
final ElementMatcher<? super TypeDescription> matcher) {
return new SafeHasSuperTypeMatcher<>(new SafeErasureMatcher<>(matcher), false);
}

/**
Expand Down Expand Up @@ -104,13 +99,17 @@ public static class SafeHasSuperTypeMatcher<T extends TypeDescription>
/** The matcher to apply to any super type of the matched type. */
private final ElementMatcher<? super TypeDescription.Generic> matcher;

private final boolean interfacesOnly;
/**
* Creates a new matcher for a super type.
*
* @param matcher The matcher to apply to any super type of the matched type.
*/
public SafeHasSuperTypeMatcher(final ElementMatcher<? super TypeDescription.Generic> matcher) {
public SafeHasSuperTypeMatcher(
final ElementMatcher<? super TypeDescription.Generic> matcher,
final boolean interfacesOnly) {
this.matcher = matcher;
this.interfacesOnly = interfacesOnly;
}

@Override
Expand All @@ -120,7 +119,8 @@ public boolean matches(final T target) {
// in {@code getSuperClass} calls
TypeDefinition typeDefinition = target;
while (typeDefinition != null) {
if (matcher.matches(typeDefinition.asGenericType())
if (((!interfacesOnly || typeDefinition.isInterface())
&& matcher.matches(typeDefinition.asGenericType()))
|| hasInterface(typeDefinition, checkedInterfaces)) {
return true;
}
Expand All @@ -129,19 +129,6 @@ public boolean matches(final T target) {
return false;
}

private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
try {
return typeDefinition.getSuperClass();
} catch (final Exception e) {
log.debug(
"{} trying to get super class for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
return null;
}
}

/**
* Matches a type's interfaces against the provided matcher.
*
Expand Down Expand Up @@ -361,22 +348,46 @@ private boolean matchesInterface(
return false;
}

private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
try {
return typeDefinition.getSuperClass();
} catch (final Exception e) {
log.debug(
"{} trying to get super class for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
return null;
}
}

@Override
public String toString() {
return "hasSuperMethodMatcher(" + matcher + ")";
}
}

private static TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
try {
return typeDefinition.getSuperClass();
} catch (final Exception e) {
log.debug(
"{} trying to get super class for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
return null;
}
}

public static class SafeExtendsClassMatcher<T extends TypeDescription>
extends ElementMatcher.Junction.AbstractBase<T> {

private final ElementMatcher<? super TypeDescription.Generic> matcher;

public SafeExtendsClassMatcher(final ElementMatcher<? super TypeDescription.Generic> matcher) {
this.matcher = matcher;
}

@Override
public boolean matches(final T target) {
// We do not use foreach loop and iterator interface here because we need to catch exceptions
// in {@code getSuperClass} calls
TypeDefinition typeDefinition = target;
while (typeDefinition != null) {
if (matcher.matches(typeDefinition.asGenericType())) {
return true;
}
typeDefinition = safeGetSuperClass(typeDefinition);
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.apachehttpasyncclient;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
Expand Down Expand Up @@ -40,7 +40,7 @@ public ApacheHttpAsyncClientInstrumentation() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(named("org.apache.http.nio.client.HttpAsyncClient"));
return safeHasInterface(named("org.apache.http.nio.client.HttpAsyncClient"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.apachehttpasyncclient;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand Down Expand Up @@ -31,7 +31,7 @@ public ApacheHttpClientRedirectInstrumentation() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(named("org.apache.http.client.RedirectStrategy"));
return safeHasInterface(named("org.apache.http.client.RedirectStrategy"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.apachehttpclient;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
Expand Down Expand Up @@ -44,7 +44,7 @@ public ApacheHttpClientInstrumentation() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.apache.http.client.HttpClient")));
return not(isInterface()).and(safeHasInterface(named("org.apache.http.client.HttpClient")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.aws.v0;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeExtendsClass;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
Expand All @@ -26,7 +26,7 @@ public RequestInstrumentation() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(named("com.amazonaws.AmazonWebServiceRequest"));
return safeExtendsClass(named("com.amazonaws.AmazonWebServiceRequest"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.aws.v2;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -23,8 +23,9 @@ public final class AwsClientInstrumentation extends AbstractAwsClientInstrumenta

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(named("software.amazon.awssdk.core.client.builder.SdkClientBuilder"))
.and(not(isInterface()));
return not(isInterface())
.and(
safeHasInterface(named("software.amazon.awssdk.core.client.builder.SdkClientBuilder")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.aws.v2;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeExtendsClass;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand Down Expand Up @@ -28,7 +28,7 @@ public final class AwsHttpClientInstrumentation extends AbstractAwsClientInstrum

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(
return safeExtendsClass(
named("software.amazon.awssdk.core.internal.http.pipeline.stages.MakeHttpRequestStage")
.or(
named(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.classloading;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeExtendsClass;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isProtected;
Expand Down Expand Up @@ -44,7 +44,7 @@ public ElementMatcher<TypeDescription> typeMatcher() {
return not(named("java.lang.ClassLoader"))
.and(not(named("com.ibm.oti.vm.BootstrapClassLoader")))
.and(not(named("datadog.trace.bootstrap.AgentClassLoader")))
.and(safeHasSuperType(named("java.lang.ClassLoader")));
.and(safeExtendsClass(named("java.lang.ClassLoader")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.couchbase.client;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeExtendsClass;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand Down Expand Up @@ -31,7 +31,7 @@ public CouchbaseNetworkInstrumentation() {
@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
// Exact class because private fields are used
return safeHasSuperType(named("com.couchbase.client.core.endpoint.AbstractGenericHandler"));
return safeExtendsClass(named("com.couchbase.client.core.endpoint.AbstractGenericHandler"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.dropwizard.view;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
Expand Down Expand Up @@ -34,7 +34,7 @@ public DropwizardViewInstrumentation() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("io.dropwizard.views.ViewRenderer")));
return not(isInterface()).and(safeHasInterface(named("io.dropwizard.views.ViewRenderer")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.finatra;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeExtendsClass;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
Expand Down Expand Up @@ -52,7 +52,7 @@ public String[] helperClassNames() {
@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
return not(isInterface())
.and(safeHasSuperType(named("com.twitter.finatra.http.internal.routing.Route")));
.and(safeExtendsClass(named("com.twitter.finatra.http.internal.routing.Route")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.hibernate.core.v3_3;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand Down Expand Up @@ -31,7 +31,7 @@ public Map<String, String> contextStore() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Criteria")));
return not(isInterface()).and(safeHasInterface(named("org.hibernate.Criteria")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.hibernate.core.v3_3;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static datadog.trace.instrumentation.hibernate.HibernateDecorator.DECORATOR;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
Expand Down Expand Up @@ -33,7 +33,7 @@ public Map<String, String> contextStore() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.Query")));
return not(isInterface()).and(safeHasInterface(named("org.hibernate.Query")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.hibernate.core.v3_3;

import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasInterface;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.hibernate.HibernateDecorator.DECORATOR;
import static java.util.Collections.singletonMap;
Expand Down Expand Up @@ -40,7 +40,7 @@ public Map<String, String> contextStore() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return not(isInterface()).and(safeHasSuperType(named("org.hibernate.SessionFactory")));
return not(isInterface()).and(safeHasInterface(named("org.hibernate.SessionFactory")));
}

@Override
Expand All @@ -53,7 +53,7 @@ public Map<? extends ElementMatcher<? super MethodDescription>, String> transfor
returns(
named("org.hibernate.Session")
.or(named("org.hibernate.StatelessSession"))
.or(safeHasSuperType(named("org.hibernate.Session"))))),
.or(safeHasInterface(named("org.hibernate.Session"))))),
SessionFactoryInstrumentation.class.getName() + "$SessionFactoryAdvice");
}

Expand Down
Loading