diff --git a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/ByteBuddyElementMatchers.java b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/ByteBuddyElementMatchers.java index a2a5d0763e..e1e0ac9f5a 100644 --- a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/ByteBuddyElementMatchers.java +++ b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/ByteBuddyElementMatchers.java @@ -24,7 +24,7 @@ public class ByteBuddyElementMatchers { public static ElementMatcher.Junction safeExtendsClass( final ElementMatcher matcher) { - return safeHasSuperType(matcher); + return new SafeExtendsClassMatcher<>(new SafeErasureMatcher<>(matcher)); } public static ElementMatcher.Junction safeHasInterface( @@ -44,22 +44,7 @@ public class ByteBuddyElementMatchers { */ public static ElementMatcher.Junction safeHasSuperType( final ElementMatcher matcher) { - return safeHasGenericSuperType(new SafeErasureMatcher(matcher)); - } - - /** - * 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 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) - */ - public static ElementMatcher.Junction safeHasGenericSuperType( - final ElementMatcher matcher) { - return new SafeHasSuperTypeMatcher<>(matcher); + return new SafeHasSuperTypeMatcher<>(new SafeErasureMatcher<>(matcher)); } /** @@ -139,19 +124,6 @@ public class ByteBuddyElementMatchers { 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. * @@ -371,22 +343,46 @@ public class ByteBuddyElementMatchers { 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 + extends ElementMatcher.Junction.AbstractBase { + + private final ElementMatcher matcher; + + public SafeExtendsClassMatcher(final ElementMatcher 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; + } + } }