private Converter<Object> provide(BeanProperty property, Genson genson) { // contextual converters must not be retrieved from cache nor stored in cache, by first // trying to create it and reusing it during the // call to genson.provideConverter we avoid retrieving it from cache, and by setting // DO_NOT_CACHE_CONVERTER to true we tell genson not to store // this converter in cache @SuppressWarnings("unchecked") Converter<Object> converter = (Converter<Object>) contextualConverterFactory.provide(property, genson); if (converter != null) { ThreadLocalHolder.store(DO_NOT_CACHE_CONVERTER_KEY, true); ThreadLocalHolder.store(CONTEXT_KEY, converter); } try { return genson.provideConverter(property.type); } finally { if (converter != null) { ThreadLocalHolder.remove(DO_NOT_CACHE_CONVERTER_KEY, Boolean.class); ThreadLocalHolder.remove(CONTEXT_KEY, Converter.class); } } }
@Override public Converter<?> create(Type type, Genson genson) { Converter<?> converter = ThreadLocalHolder.get(CONTEXT_KEY, Converter.class); if (converter != null) return converter; return delegatedFactory.create(type, genson); }