AutoBeans: synchronize static Maps used for caching

Contributed by tbroyer
Fixes issue 7667
Review at https://gwt-code-reviews.appspot.com/1829804/

Review by: cromwellian@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@11268 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/web/bindery/autobean/shared/impl/AutoBeanCodexImpl.java b/user/src/com/google/web/bindery/autobean/shared/impl/AutoBeanCodexImpl.java
index 78b4f2e..5170f2b 100644
--- a/user/src/com/google/web/bindery/autobean/shared/impl/AutoBeanCodexImpl.java
+++ b/user/src/com/google/web/bindery/autobean/shared/impl/AutoBeanCodexImpl.java
@@ -515,16 +515,18 @@
   }
 
   public static Coder doCoderFor(AutoBean<?> bean, String propertyName) {
-    String key = key(bean, propertyName);
-    Coder toReturn = coderFor.get(key);
-    if (toReturn == null) {
-      bean.accept(new PropertyCoderCreator());
-      toReturn = coderFor.get(key);
+    synchronized (coderFor) {
+      String key = key(bean, propertyName);
+      Coder toReturn = coderFor.get(key);
       if (toReturn == null) {
-        throw new IllegalArgumentException(propertyName);
+        bean.accept(new PropertyCoderCreator());
+        toReturn = coderFor.get(key);
+        if (toReturn == null) {
+          throw new IllegalArgumentException(propertyName);
+        }
       }
+      return toReturn;
     }
-    return toReturn;
   }
 
   public static <T> AutoBean<T> doDecode(EncodeState state, Class<T> clazz, Splittable data) {
@@ -562,12 +564,14 @@
   }
 
   public static <E extends Enum<?>> Coder enumCoder(Class<E> type) {
-    Coder toReturn = coders.get(type);
-    if (toReturn == null) {
-      toReturn = new EnumCoder<E>(type);
-      coders.put(type, toReturn);
+    synchronized (coders) {
+      Coder toReturn = coders.get(type);
+      if (toReturn == null) {
+        toReturn = new EnumCoder<E>(type);
+        coders.put(type, toReturn);
+      }
+      return toReturn;
     }
-    return toReturn;
   }
 
   public static Coder mapCoder(Coder valueCoder, Coder keyCoder) {
@@ -575,12 +579,14 @@
   }
 
   public static Coder objectCoder(Class<?> type) {
-    Coder toReturn = coders.get(type);
-    if (toReturn == null) {
-      toReturn = new ObjectCoder(type);
-      coders.put(type, toReturn);
+    synchronized (coders) {
+      Coder toReturn = coders.get(type);
+      if (toReturn == null) {
+        toReturn = new ObjectCoder(type);
+        coders.put(type, toReturn);
+      }
+      return toReturn;
     }
-    return toReturn;
   }
 
   public static Coder splittableCoder() {
@@ -588,12 +594,14 @@
   }
 
   public static Coder valueCoder(Class<?> type) {
-    Coder toReturn = coders.get(type);
-    if (toReturn == null) {
-      toReturn = new ValueCoder(type);
-      coders.put(type, toReturn);
+    synchronized (coders) {
+      Coder toReturn = coders.get(type);
+      if (toReturn == null) {
+        toReturn = new ValueCoder(type);
+        coders.put(type, toReturn);
+      }
+      return toReturn;
     }
-    return toReturn;
   }
 
   static Splittable tryExtractSplittable(Object value) {