Skip to content

Commit 3aa9ac1

Browse files
committed
Update cache to support concurrent reads
Change the cache implementation from a synchronized weak hash map to a concurrent implementation. Issue: SPR-8701
1 parent 02ce826 commit 3aa9ac1

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,22 @@
1616

1717
package org.springframework.core;
1818

19-
import java.lang.ref.Reference;
20-
import java.lang.ref.WeakReference;
2119
import java.lang.reflect.Array;
2220
import java.lang.reflect.GenericArrayType;
2321
import java.lang.reflect.Method;
2422
import java.lang.reflect.ParameterizedType;
2523
import java.lang.reflect.Type;
2624
import java.lang.reflect.TypeVariable;
2725
import java.lang.reflect.WildcardType;
28-
import java.util.Collections;
2926
import java.util.HashMap;
3027
import java.util.Map;
31-
import java.util.WeakHashMap;
3228

3329
import org.apache.commons.logging.Log;
3430
import org.apache.commons.logging.LogFactory;
3531

3632
import org.springframework.util.Assert;
3733
import org.springframework.util.ObjectUtils;
34+
import org.springframework.util.ConcurrentReferenceHashMap;
3835

3936
/**
4037
* Helper class for resolving generic types against type variables.
@@ -53,9 +50,8 @@ public abstract class GenericTypeResolver {
5350
private static final Log logger = LogFactory.getLog(GenericTypeResolver.class);
5451

5552
/** Cache from Class to TypeVariable Map */
56-
private static final Map<Class, Reference<Map<TypeVariable, Type>>> typeVariableCache =
57-
Collections.synchronizedMap(new WeakHashMap<Class, Reference<Map<TypeVariable, Type>>>());
58-
53+
private static final Map<Class, Map<TypeVariable, Type>> typeVariableCache =
54+
new ConcurrentReferenceHashMap<Class, Map<TypeVariable,Type>>();
5955

6056
/**
6157
* Determine the target type for the given parameter specification.
@@ -408,8 +404,8 @@ static Type getRawType(Type genericType, Map<TypeVariable, Type> typeVariableMap
408404
* all super types, enclosing types and interfaces.
409405
*/
410406
public static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
411-
Reference<Map<TypeVariable, Type>> ref = typeVariableCache.get(clazz);
412-
Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref.get() : null);
407+
Map<TypeVariable, Type> ref = typeVariableCache.get(clazz);
408+
Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref : null);
413409

414410
if (typeVariableMap == null) {
415411
typeVariableMap = new HashMap<TypeVariable, Type>();
@@ -441,7 +437,7 @@ public static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
441437
type = type.getEnclosingClass();
442438
}
443439

444-
typeVariableCache.put(clazz, new WeakReference<Map<TypeVariable, Type>>(typeVariableMap));
440+
typeVariableCache.put(clazz, typeVariableMap);
445441
}
446442

447443
return typeVariableMap;

0 commit comments

Comments
 (0)