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 8588bbae7d..8cb4d1fb01 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 @@ -2,13 +2,15 @@ package datadog.trace.agent.tooling; import static net.bytebuddy.matcher.ElementMatchers.erasure; +import java.util.ArrayList; import java.util.HashSet; +import java.util.Iterator; +import java.util.List; import java.util.Set; import lombok.extern.slf4j.Slf4j; import net.bytebuddy.build.HashCodeAndEqualsPlugin; import net.bytebuddy.description.type.TypeDefinition; import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.description.type.TypeList; import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatchers; @@ -89,7 +91,7 @@ public class ByteBuddyElementMatchers { // in {@code getSuperClass} calls TypeDefinition typeDefinition = target; while (typeDefinition != null) { - if (matcher.matches(typeDefinition.asGenericType()) + if (safeMatches(typeDefinition.asGenericType()) || hasInterface(typeDefinition, checkedInterfaces)) { return true; } @@ -98,11 +100,20 @@ public class ByteBuddyElementMatchers { return false; } + private boolean safeMatches(TypeDescription.Generic target) { + try { + return matcher.matches(target); + } catch (final Exception e) { + log.debug(matcher + ": Exception trying to get match generic type description:", e); + } + return false; + } + private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) { try { return typeDefinition.getSuperClass(); } catch (final Exception e) { - log.info("Exception trying to get next type definition:", e); + log.debug("Exception trying to get next type definition:", e); return null; } } @@ -118,7 +129,7 @@ public class ByteBuddyElementMatchers { final TypeDefinition typeDefinition, final Set checkedInterfaces) { for (final TypeDefinition interfaceType : safeGetInterfaces(typeDefinition)) { if (checkedInterfaces.add(interfaceType.asErasure()) - && (matcher.matches(interfaceType.asGenericType()) + && (safeMatch(interfaceType.asGenericType()) || hasInterface(interfaceType, checkedInterfaces))) { return true; } @@ -126,13 +137,27 @@ public class ByteBuddyElementMatchers { return false; } - private TypeList.Generic safeGetInterfaces(final TypeDefinition typeDefinition) { + private boolean safeMatch(TypeDescription.Generic target) { try { - return typeDefinition.getInterfaces(); + return matcher.matches(target); } catch (final Exception e) { - log.info("Exception trying to get interfaces:", e); - return new TypeList.Generic.Empty(); + log.debug(matcher + ": Exception while matching:", e); } + return false; + } + + private List safeGetInterfaces(final TypeDefinition typeDefinition) { + final List interfaceTypes = new ArrayList<>(); + try { + final Iterator interfaceIter = + typeDefinition.getInterfaces().iterator(); + while (interfaceIter.hasNext()) { + interfaceTypes.add(interfaceIter.next()); + } + } catch (final Exception e) { + log.debug("Exception trying to get interfaces:", e); + } + return interfaceTypes; } @Override diff --git a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/DDCachingPoolStrategy.java b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/DDCachingPoolStrategy.java index b6f9aba630..55bba83375 100644 --- a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/DDCachingPoolStrategy.java +++ b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/DDCachingPoolStrategy.java @@ -9,22 +9,25 @@ import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.pool.TypePool; public class DDCachingPoolStrategy implements PoolStrategy { - private static final Map typePoolCache = - Collections.synchronizedMap(new WeakHashMap()); + private static final Map typePoolCache = + Collections.synchronizedMap(new WeakHashMap()); @Override public TypePool typePool(ClassFileLocator classFileLocator, ClassLoader classLoader) { final ClassLoader key = null == classLoader ? Utils.getBootstrapProxy() : classLoader; - TypePool cachedPool = typePoolCache.get(key); - if (null == cachedPool) { + TypePool.CacheProvider cache = typePoolCache.get(key); + if (null == cache) { synchronized (key) { - cachedPool = typePoolCache.get(key); - if (null == cachedPool) { - cachedPool = Default.FAST.typePool(classFileLocator, classLoader); - typePoolCache.put(key, cachedPool); + cache = typePoolCache.get(key); + if (null == cache) { + // new TypePool.Default.WithLazyResolution(TypePool.CacheProvider.Simple.withObjectType(), + // classFileLocator, TypePool.Default.ReaderMode.FAST); + cache = TypePool.CacheProvider.Simple.withObjectType(); + typePoolCache.put(key, cache); } } } - return cachedPool; + return new TypePool.Default.WithLazyResolution( + cache, classFileLocator, TypePool.Default.ReaderMode.FAST); } } diff --git a/dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java b/dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java index 96d1e8f348..e044458581 100644 --- a/dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java +++ b/dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java @@ -140,7 +140,8 @@ public abstract class AgentTestRunner extends Specification { @After public void afterTest() { ERROR_LISTENER.deactivateTest(this); - assert INSTRUMENTATION_ERROR_COUNT.get() == 0 : "Instrumentation errors during test"; + assert INSTRUMENTATION_ERROR_COUNT.get() == 0 + : INSTRUMENTATION_ERROR_COUNT.get() + " Instrumentation errors during test"; } @AfterClass