Skip to content

Commit

Permalink
Added a simple, map based binary locator for a caching type pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Apr 19, 2016
1 parent 1967cba commit eba608d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.security.ProtectionDomain;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import static net.bytebuddy.matcher.ElementMatchers.*;
Expand Down Expand Up @@ -1053,6 +1054,62 @@ public boolean equals(Object object) {
public int hashCode() {
return readerMode.hashCode();
}

/**
* An implementation of a binary locator {@link WithTypePoolCache} (note documentation of the linked class) that is based on a
* {@link ConcurrentMap}. It is the responsibility of the binary locator's user to avoid the binary locator from leaking memory.
*/
public static class Simple extends WithTypePoolCache {

/**
* The concurrent map that is used for storing a cache provider per class loader.
*/
private final ConcurrentMap<? super ClassLoader, TypePool.CacheProvider> cacheProviders;

/**
* Creates a new binary locator that caches a cache provider per class loader in a concurrent map.
*
* @param readerMode The reader mode to use for parsing a class file.
* @param cacheProviders The concurrent map that is used for storing a cache provider per class loader.
*/
public Simple(TypePool.Default.ReaderMode readerMode, ConcurrentMap<? super ClassLoader, TypePool.CacheProvider> cacheProviders) {
super(readerMode);
this.cacheProviders = cacheProviders;
}

@Override
protected TypePool.CacheProvider locate(ClassLoader classLoader) {
TypePool.CacheProvider cacheProvider = cacheProviders.get(classLoader);
while (cacheProvider == null) {
cacheProviders.putIfAbsent(classLoader, new TypePool.CacheProvider.Simple());
cacheProvider = cacheProviders.get(classLoader);
}
return cacheProvider;
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
if (!super.equals(object)) return false;
Simple simple = (Simple) object;
return cacheProviders.equals(simple.cacheProviders);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + cacheProviders.hashCode();
return result;
}

@Override
public String toString() {
return "AgentBuilder.BinaryLocator.WithTypePoolCache.Simple{" +
"cacheProviders=" + cacheProviders +
'}';
}
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.bytebuddy.agent.builder;

import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.test.utility.MockitoRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.mockito.Mock;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;


public class AgentBuilderBinaryLocatorWithTypePoolCacheSimpleTest {

@Rule
public TestRule mockitoRule = new MockitoRule(this);

@Mock
private ClassFileLocator classFileLocator;

@Mock
private ClassLoader first, second;

@Test
public void testSimpleImplementation() throws Exception {
ConcurrentMap<ClassLoader, TypePool.CacheProvider> cacheProviders = new ConcurrentHashMap<ClassLoader, TypePool.CacheProvider>();
AgentBuilder.BinaryLocator binaryLocator = new AgentBuilder.BinaryLocator.WithTypePoolCache.Simple(TypePool.Default.ReaderMode.FAST, cacheProviders);
assertThat(binaryLocator.typePool(classFileLocator, first), is(binaryLocator.typePool(classFileLocator, first)));
assertThat(binaryLocator.typePool(classFileLocator, first), not(binaryLocator.typePool(classFileLocator, second)));
}

@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(AgentBuilder.BinaryLocator.WithTypePoolCache.Simple.class).apply();
}
}

0 comments on commit eba608d

Please sign in to comment.