From 1e18dce002158992bbda1d3a19952fdf07f308f4 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Tue, 23 Oct 2012 02:41:34 +0000 Subject: [PATCH] Don't subclass ThreadLocal. This attempts to address issue 402, wherein subclassing ThreadLocal is pinning a reference to a class, which transitively pins the entire application in containers like Tomcat. --- gson/src/main/java/com/google/gson/Gson.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index da1bf437..aa80aeae 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -111,11 +111,7 @@ public final class Gson { * The proxy is wired up once the initial adapter has been created. */ private final ThreadLocal, FutureTypeAdapter>> calls - = new ThreadLocal, FutureTypeAdapter>>() { - @Override protected Map, FutureTypeAdapter> initialValue() { - return new HashMap, FutureTypeAdapter>(); - } - }; + = new ThreadLocal, FutureTypeAdapter>>(); private final Map, TypeAdapter> typeTokenCache = Collections.synchronizedMap(new HashMap, TypeAdapter>()); @@ -343,6 +339,11 @@ public final class Gson { } Map, FutureTypeAdapter> threadCalls = calls.get(); + if (threadCalls == null) { + threadCalls = new HashMap, FutureTypeAdapter>(); + calls.set(threadCalls); + } + // the key and value type parameters always agree FutureTypeAdapter ongoingCall = (FutureTypeAdapter) threadCalls.get(type); if (ongoingCall != null) {