From dc954f774b3358427d547674293e6f4ac18eeb7a Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Mon, 19 Dec 2016 19:09:49 +0100 Subject: [PATCH] Improved type cache. --- .../main/java/net/bytebuddy/TypeCache.java | 47 ++++--------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/TypeCache.java b/byte-buddy-dep/src/main/java/net/bytebuddy/TypeCache.java index 76f9166a178..f863062f138 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/TypeCache.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/TypeCache.java @@ -2,13 +2,10 @@ import net.bytebuddy.utility.CompoundList; -import java.io.Serializable; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; -import java.net.URL; -import java.net.URLClassLoader; import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; @@ -45,11 +42,6 @@ public class TypeCache extends ReferenceQueue { */ private static final Class NOT_FOUND = null; - /** - * Represents the boot class loader which is {@code null} and which is therefore incompatible with system hash code computation. - */ - private static final ClassLoader BOOT_LOADER_REPRESENTATIVE = new URLClassLoader(new URL[0]); - /** * The reference type to use for stored types. */ @@ -78,7 +70,7 @@ public TypeCache(Sort sort) { * @return The stored type or {@code null} if no type was stored. */ public Class find(ClassLoader classLoader, T key) { - ConcurrentMap>> storage = cache.get(LookupKey.of(classLoader)); + ConcurrentMap>> storage = cache.get(new LookupKey(classLoader)); if (storage == null) { return NOT_FOUND; } else { @@ -100,10 +92,10 @@ public Class find(ClassLoader classLoader, T key) { * @return The supplied type or a previously submitted type for the same class loader and key combination. */ public Class insert(ClassLoader classLoader, T key, Class type) { - ConcurrentMap>> storage = cache.get(LookupKey.of(classLoader)); + ConcurrentMap>> storage = cache.get(new LookupKey(classLoader)); if (storage == null) { storage = new ConcurrentHashMap>>(); - ConcurrentMap>> previous = cache.putIfAbsent(StorageKey.of(classLoader, this), storage); + ConcurrentMap>> previous = cache.putIfAbsent(new StorageKey(classLoader, this), storage); if (previous != null) { storage = previous; } @@ -249,23 +241,13 @@ protected static class LookupKey { /** * Creates a new lookup key. * - * @param classLoader A class loader representing the referenced class loader which is never {@code null}. + * @param classLoader The represented class loader. */ protected LookupKey(ClassLoader classLoader) { this.classLoader = classLoader; hashCode = System.identityHashCode(classLoader); } - /** - * Creates a lookup key. - * - * @param classLoader The class loader to represent. - * @return An appropriate lookup key. - */ - protected static Object of(ClassLoader classLoader) { - return new LookupKey(classLoader == null ? BOOT_LOADER_REPRESENTATIVE : classLoader); - } - @Override public int hashCode() { return hashCode; @@ -274,7 +256,7 @@ public int hashCode() { @Override public boolean equals(Object other) { return (other instanceof LookupKey && ((LookupKey) other).classLoader == classLoader) - || (other instanceof StorageKey && ((StorageKey) other).get() == classLoader); + || (other instanceof StorageKey && ((StorageKey) other).get() == classLoader && ((StorageKey) other).hashCode == hashCode); } @Override @@ -299,23 +281,12 @@ protected static class StorageKey extends WeakReference { /** * Creates a new storage key. * - * @param classLoader A class loader representing the referenced class loader which is never {@code null}. + * @param classLoader The represented class loader. * @param referenceQueue The reference queue to notify upon a garbage collection. */ protected StorageKey(ClassLoader classLoader, ReferenceQueue referenceQueue) { super(classLoader, referenceQueue); - this.hashCode = System.identityHashCode(classLoader); - } - - /** - * Creates a new storage key. - * - * @param classLoader A class loader representing the referenced class loader which is never {@code null}. - * @param referenceQueue The reference queue to notify upon a garbage collection. - * @return An appropriate storage key. - */ - protected static Object of(ClassLoader classLoader, ReferenceQueue referenceQueue) { - return new StorageKey(classLoader == null ? BOOT_LOADER_REPRESENTATIVE : classLoader, referenceQueue); + hashCode = System.identityHashCode(classLoader); } @Override @@ -325,7 +296,7 @@ public int hashCode() { @Override public boolean equals(Object other) { - return (other instanceof LookupKey && ((LookupKey) other).classLoader == get()) + return (other instanceof LookupKey && ((LookupKey) other).classLoader == get() && ((LookupKey) other).hashCode == hashCode) || (other instanceof StorageKey && ((StorageKey) other).get() == get()); } @@ -405,7 +376,7 @@ public String toString() { /** * A simple key based on a collection of types where no type is strongly referenced. */ - public static class SimpleKey implements Serializable { + public static class SimpleKey { /** * The referenced types.