@@ -204,20 +204,27 @@ public <T> TypeSerializer<T> getTypeSerializer(TypeToken<T> type) {
204204 serializer = (TypeSerializer <T >) typeSerializerMap .get (type );
205205 if (serializer != null ) return serializer ;
206206
207- FutureTypeSerializer <T > future = new FutureTypeSerializer <>();
208- typeSerializerMap .put (type , future ); // set future before creation of new serializers to avoid recursive creation
209-
210- for (int i = serializerFactories .size () - 1 ; i >= 0 ; i --) {
211- TypeSerializerFactory factory = serializerFactories .get (i );
212- serializer = factory .create (type , this ).orElse (null );
213- if (serializer != null ) break ;
207+ try {
208+ FutureTypeSerializer <T > future = new FutureTypeSerializer <>();
209+ typeSerializerMap .put (type , future ); // set future before creation of new serializers to avoid recursive creation
210+
211+ for (int i = serializerFactories .size () - 1 ; i >= 0 ; i --) {
212+ TypeSerializerFactory factory = serializerFactories .get (i );
213+ serializer = factory .create (type , this ).orElse (null );
214+ if (serializer != null ) break ;
215+ }
216+
217+ if (serializer == null )
218+ serializer = DefaultSerializerFactory .INSTANCE .createFor (type , this );
219+
220+ future .complete (serializer );
221+ } finally {
222+ if (serializer != null ) {
223+ typeSerializerMap .put (type , serializer );
224+ } else {
225+ typeSerializerMap .remove (type );
226+ }
214227 }
215-
216- if (serializer == null )
217- serializer = DefaultSerializerFactory .INSTANCE .createFor (type , this );
218-
219- future .complete (serializer );
220- typeSerializerMap .put (type , serializer );
221228 }
222229
223230 return serializer ;
@@ -235,20 +242,27 @@ public <T> TypeDeserializer<T> getTypeDeserializer(TypeToken<T> type) {
235242 deserializer = (TypeDeserializer <T >) typeDeserializerMap .get (type );
236243 if (deserializer != null ) return deserializer ;
237244
238- FutureTypeDeserializer <T > future = new FutureTypeDeserializer <>();
239- typeDeserializerMap .put (type , future ); // set future before creation of new deserializers to avoid recursive creation
240-
241- for (int i = deserializerFactories .size () - 1 ; i >= 0 ; i --) {
242- TypeDeserializerFactory factory = deserializerFactories .get (i );
243- deserializer = factory .create (type , this ).orElse (null );
244- if (deserializer != null ) break ;
245+ try {
246+ FutureTypeDeserializer <T > future = new FutureTypeDeserializer <>();
247+ typeDeserializerMap .put (type , future ); // set future before creation of new deserializers to avoid recursive creation
248+
249+ for (int i = deserializerFactories .size () - 1 ; i >= 0 ; i --) {
250+ TypeDeserializerFactory factory = deserializerFactories .get (i );
251+ deserializer = factory .create (type , this ).orElse (null );
252+ if (deserializer != null ) break ;
253+ }
254+
255+ if (deserializer == null )
256+ deserializer = DefaultDeserializerFactory .INSTANCE .createFor (type , this );
257+
258+ future .complete (deserializer );
259+ } finally {
260+ if (deserializer != null ) {
261+ typeDeserializerMap .put (type , deserializer );
262+ } else {
263+ typeDeserializerMap .remove (type );
264+ }
245265 }
246-
247- if (deserializer == null )
248- deserializer = DefaultDeserializerFactory .INSTANCE .createFor (type , this );
249-
250- future .complete (deserializer );
251- typeDeserializerMap .put (type , deserializer );
252266 }
253267
254268 return deserializer ;
@@ -266,20 +280,27 @@ public <T> InstanceCreator<T> getInstanceCreator(TypeToken<T> type) {
266280 instanceCreator = (InstanceCreator <T >) instanceCreatorMap .get (type );
267281 if (instanceCreator != null ) return instanceCreator ;
268282
269- FutureInstanceCreator <T > future = new FutureInstanceCreator <>();
270- instanceCreatorMap .put (type , future ); // set future before creation of new deserializers to avoid recursive creation
271-
272- for (int i = instanceCreatorFactories .size () - 1 ; i >= 0 ; i --) {
273- InstanceCreatorFactory factory = instanceCreatorFactories .get (i );
274- instanceCreator = factory .create (type , this ).orElse (null );
275- if (instanceCreator != null ) break ;
283+ try {
284+ FutureInstanceCreator <T > future = new FutureInstanceCreator <>();
285+ instanceCreatorMap .put (type , future ); // set future before creation of new deserializers to avoid recursive creation
286+
287+ for (int i = instanceCreatorFactories .size () - 1 ; i >= 0 ; i --) {
288+ InstanceCreatorFactory factory = instanceCreatorFactories .get (i );
289+ instanceCreator = factory .create (type , this ).orElse (null );
290+ if (instanceCreator != null ) break ;
291+ }
292+
293+ if (instanceCreator == null )
294+ instanceCreator = DefaultInstanceCreatorFactory .INSTANCE .createFor (type , this );
295+
296+ future .complete (instanceCreator );
297+ } finally {
298+ if (instanceCreator != null ) {
299+ instanceCreatorMap .put (type , instanceCreator );
300+ } else {
301+ instanceCreatorMap .remove (type );
302+ }
276303 }
277-
278- if (instanceCreator == null )
279- instanceCreator = DefaultInstanceCreatorFactory .INSTANCE .createFor (type , this );
280-
281- future .complete (instanceCreator );
282- instanceCreatorMap .put (type , instanceCreator );
283304 }
284305
285306 return instanceCreator ;
@@ -297,20 +318,27 @@ public <T> InstanceCreator<T> getInstanceCreator(TypeToken<T> type) {
297318 typeResolver = (TypeResolver <T , ?>) typeResolverMap .get (type );
298319 if (typeResolver != null ) return typeResolver ;
299320
300- FutureTypeResolver <T , ?> future = new FutureTypeResolver <>();
301- typeResolverMap .put (type , future ); // set future before creation of new deserializers to avoid recursive creation
302-
303- for (int i = typeResolverFactories .size () - 1 ; i >= 0 ; i --) {
304- TypeResolverFactory factory = typeResolverFactories .get (i );
305- typeResolver = factory .create (type , this ).orElse (null );
306- if (typeResolver != null ) break ;
321+ try {
322+ FutureTypeResolver <T , ?> future = new FutureTypeResolver <>();
323+ typeResolverMap .put (type , future ); // set future before creation of new deserializers to avoid recursive creation
324+
325+ for (int i = typeResolverFactories .size () - 1 ; i >= 0 ; i --) {
326+ TypeResolverFactory factory = typeResolverFactories .get (i );
327+ typeResolver = factory .create (type , this ).orElse (null );
328+ if (typeResolver != null ) break ;
329+ }
330+
331+ if (typeResolver == null )
332+ typeResolver = NO_TYPE_RESOLVER ;
333+
334+ future .complete ((TypeResolver ) typeResolver );
335+ } finally {
336+ if (typeResolver != null ) {
337+ typeResolverMap .put (type , typeResolver );
338+ } else {
339+ typeResolverMap .remove (type );
340+ }
307341 }
308-
309- if (typeResolver == null )
310- typeResolver = NO_TYPE_RESOLVER ;
311-
312- future .complete ((TypeResolver ) typeResolver );
313- typeResolverMap .put (type , typeResolver );
314342 }
315343
316344 return typeResolver == NO_TYPE_RESOLVER ? null : typeResolver ;
0 commit comments