From 3aa9ac15a1e0a36ff15b94e18bf75e4034a643f4 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 13 Sep 2012 17:38:07 -0700 Subject: [PATCH] Update cache to support concurrent reads Change the cache implementation from a synchronized weak hash map to a concurrent implementation. Issue: SPR-8701 --- .../core/GenericTypeResolver.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java index 9c025c0ba5d..1156f8ebdf3 100644 --- a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java +++ b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java @@ -16,8 +16,6 @@ package org.springframework.core; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; import java.lang.reflect.Array; import java.lang.reflect.GenericArrayType; import java.lang.reflect.Method; @@ -25,16 +23,15 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; -import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.WeakHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; +import org.springframework.util.ConcurrentReferenceHashMap; /** * Helper class for resolving generic types against type variables. @@ -53,9 +50,8 @@ public abstract class GenericTypeResolver { private static final Log logger = LogFactory.getLog(GenericTypeResolver.class); /** Cache from Class to TypeVariable Map */ - private static final Map>> typeVariableCache = - Collections.synchronizedMap(new WeakHashMap>>()); - + private static final Map> typeVariableCache = + new ConcurrentReferenceHashMap>(); /** * Determine the target type for the given parameter specification. @@ -408,8 +404,8 @@ public abstract class GenericTypeResolver { * all super types, enclosing types and interfaces. */ public static Map getTypeVariableMap(Class clazz) { - Reference> ref = typeVariableCache.get(clazz); - Map typeVariableMap = (ref != null ? ref.get() : null); + Map ref = typeVariableCache.get(clazz); + Map typeVariableMap = (ref != null ? ref : null); if (typeVariableMap == null) { typeVariableMap = new HashMap(); @@ -441,7 +437,7 @@ public abstract class GenericTypeResolver { type = type.getEnclosingClass(); } - typeVariableCache.put(clazz, new WeakReference>(typeVariableMap)); + typeVariableCache.put(clazz, typeVariableMap); } return typeVariableMap;