Browse Source

type variable cache uses weak values

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@797 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Juergen Hoeller 17 years ago
parent
commit
aecc6b45df
  1. 11
      org.springframework.core/src/main/java/org/springframework/core/GenericTypeResolver.java

11
org.springframework.core/src/main/java/org/springframework/core/GenericTypeResolver.java

@ -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;

Loading…
Cancel
Save