Remove safeMatch from element matcher and document bb cache class

This commit is contained in:
Andrew Kent 2018-08-14 12:12:59 -07:00
parent 260e3520aa
commit d64b162539
3 changed files with 25 additions and 21 deletions

View File

@ -71,6 +71,7 @@ class ClassLoadingTest extends Specification {
then: then:
// ClassToInstrumentChild won't cause an additional getResource() because its TypeDescription is created from transformation bytes. // ClassToInstrumentChild won't cause an additional getResource() because its TypeDescription is created from transformation bytes.
loader.count > 0
loader.count == countAfterFirstLoad loader.count == countAfterFirstLoad
} }

View File

@ -91,7 +91,7 @@ public class ByteBuddyElementMatchers {
// in {@code getSuperClass} calls // in {@code getSuperClass} calls
TypeDefinition typeDefinition = target; TypeDefinition typeDefinition = target;
while (typeDefinition != null) { while (typeDefinition != null) {
if (safeMatches(typeDefinition.asGenericType()) if (matcher.matches(typeDefinition.asGenericType())
|| hasInterface(typeDefinition, checkedInterfaces)) { || hasInterface(typeDefinition, checkedInterfaces)) {
return true; return true;
} }
@ -100,15 +100,6 @@ public class ByteBuddyElementMatchers {
return false; 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) { private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
try { try {
return typeDefinition.getSuperClass(); return typeDefinition.getSuperClass();
@ -129,7 +120,7 @@ public class ByteBuddyElementMatchers {
final TypeDefinition typeDefinition, final Set<TypeDescription> checkedInterfaces) { final TypeDefinition typeDefinition, final Set<TypeDescription> checkedInterfaces) {
for (final TypeDefinition interfaceType : safeGetInterfaces(typeDefinition)) { for (final TypeDefinition interfaceType : safeGetInterfaces(typeDefinition)) {
if (checkedInterfaces.add(interfaceType.asErasure()) if (checkedInterfaces.add(interfaceType.asErasure())
&& (safeMatch(interfaceType.asGenericType()) && (matcher.matches(interfaceType.asGenericType())
|| hasInterface(interfaceType, checkedInterfaces))) { || hasInterface(interfaceType, checkedInterfaces))) {
return true; return true;
} }
@ -137,15 +128,12 @@ public class ByteBuddyElementMatchers {
return false; return false;
} }
private boolean safeMatch(TypeDescription.Generic target) { /**
try { * TypeDefinition#getInterfaces() may throw an exception during iteration if an interface is
return matcher.matches(target); * absent from the classpath.
} catch (final Exception e) { *
log.debug(matcher + ": Exception while matching:", e); * <p>This method exists to allow getting interfaces even if the lookup on one fails.
} */
return false;
}
private List<TypeDefinition> safeGetInterfaces(final TypeDefinition typeDefinition) { private List<TypeDefinition> safeGetInterfaces(final TypeDefinition typeDefinition) {
final List<TypeDefinition> interfaceTypes = new ArrayList<>(); final List<TypeDefinition> interfaceTypes = new ArrayList<>();
try { try {

View File

@ -1,5 +1,6 @@
package datadog.trace.agent.tooling; package datadog.trace.agent.tooling;
import static datadog.trace.agent.tooling.ClassLoaderMatcher.BOOTSTRAP_CLASSLOADER;
import static net.bytebuddy.agent.builder.AgentBuilder.*; import static net.bytebuddy.agent.builder.AgentBuilder.*;
import java.util.Collections; import java.util.Collections;
@ -8,13 +9,27 @@ import java.util.WeakHashMap;
import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.pool.TypePool; import net.bytebuddy.pool.TypePool;
/**
* Custom Pool strategy.
*
* <p>This is similar to: AgentBuilder.PoolStrategy.WithTypePoolCache.Simple(new
* MapMaker().weakKeys().<ClassLoader, TypePool.CacheProvider>makeMap())
*
* <p>Main differences:
*
* <ol>
* <li>Control over the type of the cache. We many not want to use a java.util.ConcurrentMap
* <li>Use our bootstrap proxy when matching against the bootstrap loader.
* </ol>
*/
public class DDCachingPoolStrategy implements PoolStrategy { public class DDCachingPoolStrategy implements PoolStrategy {
private static final Map<ClassLoader, TypePool.CacheProvider> typePoolCache = private static final Map<ClassLoader, TypePool.CacheProvider> typePoolCache =
Collections.synchronizedMap(new WeakHashMap<ClassLoader, TypePool.CacheProvider>()); Collections.synchronizedMap(new WeakHashMap<ClassLoader, TypePool.CacheProvider>());
@Override @Override
public TypePool typePool(ClassFileLocator classFileLocator, ClassLoader classLoader) { public TypePool typePool(ClassFileLocator classFileLocator, ClassLoader classLoader) {
final ClassLoader key = null == classLoader ? Utils.getBootstrapProxy() : classLoader; final ClassLoader key =
BOOTSTRAP_CLASSLOADER == classLoader ? Utils.getBootstrapProxy() : classLoader;
TypePool.CacheProvider cache = typePoolCache.get(key); TypePool.CacheProvider cache = typePoolCache.get(key);
if (null == cache) { if (null == cache) {
synchronized (key) { synchronized (key) {