type variable cache uses weak values

This commit is contained in:
Juergen Hoeller 2009-03-23 11:32:03 +00:00
parent 38182f302a
commit 84cc32525f
1 changed files with 7 additions and 4 deletions

View File

@ -16,6 +16,8 @@
package org.springframework.core; package org.springframework.core;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
@ -43,8 +45,8 @@ import org.springframework.util.Assert;
public abstract class GenericTypeResolver { public abstract class GenericTypeResolver {
/** Cache from Class to TypeVariable Map */ /** Cache from Class to TypeVariable Map */
private static final Map<Class, Map<TypeVariable, Type>> typeVariableCache = private static final Map<Class, Reference<Map<TypeVariable, Type>>> typeVariableCache =
Collections.synchronizedMap(new WeakHashMap<Class, Map<TypeVariable, Type>>()); Collections.synchronizedMap(new WeakHashMap<Class, Reference<Map<TypeVariable, Type>>>());
/** /**
@ -152,7 +154,8 @@ public abstract class GenericTypeResolver {
* enclosing types and interfaces. * enclosing types and interfaces.
*/ */
static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) { static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
Map<TypeVariable, Type> typeVariableMap = typeVariableCache.get(clazz); Reference<Map<TypeVariable, Type>> ref = typeVariableCache.get(clazz);
Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref.get() : null);
if (typeVariableMap == null) { if (typeVariableMap == null) {
typeVariableMap = new HashMap<TypeVariable, Type>(); typeVariableMap = new HashMap<TypeVariable, Type>();
@ -184,7 +187,7 @@ public abstract class GenericTypeResolver {
type = type.getEnclosingClass(); type = type.getEnclosingClass();
} }
typeVariableCache.put(clazz, typeVariableMap); typeVariableCache.put(clazz, new WeakReference<Map<TypeVariable, Type>>(typeVariableMap));
} }
return typeVariableMap; return typeVariableMap;