@@ -2,14 +2,14 @@ package com.fasterxml.jackson.module.scala.introspect
2
2
3
3
import com .fasterxml .jackson .annotation .JsonCreator
4
4
import com .fasterxml .jackson .core .Version
5
- import com .fasterxml .jackson .databind .`type` .{ClassKey , CollectionLikeType , MapLikeType , ReferenceType , SimpleType }
5
+ import com .fasterxml .jackson .databind .`type` .{CollectionLikeType , MapLikeType , ReferenceType , SimpleType }
6
6
import com .fasterxml .jackson .databind .cfg .MapperConfig
7
7
import com .fasterxml .jackson .databind .deser .std .StdValueInstantiator
8
8
import com .fasterxml .jackson .databind .deser ._
9
9
import com .fasterxml .jackson .databind .introspect ._
10
- import com .fasterxml .jackson .databind .util .{AccessPattern , LRUMap , LookupCache }
10
+ import com .fasterxml .jackson .databind .util .{AccessPattern , LookupCache }
11
11
import com .fasterxml .jackson .databind .{BeanDescription , DeserializationConfig , DeserializationContext , JavaType , MapperFeature }
12
- import com .fasterxml .jackson .module .scala .JacksonModule
12
+ import com .fasterxml .jackson .module .scala .{ DefaultLookupCacheFactory , JacksonModule , LookupCacheFactory }
13
13
import com .fasterxml .jackson .module .scala .util .Implicits ._
14
14
15
15
import java .lang .annotation .Annotation
@@ -178,7 +178,7 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
178
178
}
179
179
180
180
private def _descriptorFor (clz : Class [_]): Option [BeanDescriptor ] = {
181
- val key = new ClassKey ( clz)
181
+ val key = clz.getName
182
182
val isScala = {
183
183
Option (ScalaAnnotationIntrospectorModule ._scalaTypeCache.get(key)) match {
184
184
case Some (result) => result
@@ -247,15 +247,80 @@ trait ScalaAnnotationIntrospectorModule extends JacksonModule {
247
247
this += { _.appendAnnotationIntrospector(ScalaAnnotationIntrospector ) }
248
248
this += { _.addValueInstantiators(ScalaAnnotationIntrospector ) }
249
249
250
- private [introspect] var _descriptorCache : LookupCache [ClassKey , BeanDescriptor ] =
251
- new LRUMap [ClassKey , BeanDescriptor ](16 , 100 )
250
+ private var _lookupCacheFactory : LookupCacheFactory = DefaultLookupCacheFactory
251
+ private var _shouldSupportScala3Classes : Boolean = true
252
+ private var _descriptorCacheSize : Int = 100
253
+ private var _scalaTypeCacheSize : Int = 1000
252
254
253
- private [introspect] var _scalaTypeCache : LookupCache [ClassKey , Boolean ] =
254
- new LRUMap [ClassKey , Boolean ](16 , 1000 )
255
+ private [introspect] var _descriptorCache : LookupCache [String , BeanDescriptor ] =
256
+ _lookupCacheFactory.createLookupCache(16 , _descriptorCacheSize)
257
+
258
+ private [introspect] var _scalaTypeCache : LookupCache [String , Boolean ] =
259
+ _lookupCacheFactory.createLookupCache(16 , _scalaTypeCacheSize)
255
260
256
261
private [introspect] val overrideMap = MutableMap [Class [_], ClassOverrides ]()
257
262
258
- private var _shouldSupportScala3Classes = true
263
+ /**
264
+ * Replaces the [[LookupCacheFactory ]]. The default factory uses [[com.fasterxml.jackson.databind.util.LRUMap ]].
265
+ * <p>
266
+ * Note that this clears the existing cache entries. It is best to set this up before you start using
267
+ * the Jackson Scala Module for serializing/deserializing.
268
+ * </p>
269
+ *
270
+ * @param lookupCacheFactory new factory
271
+ * @see [[setDescriptorCacheSize ]]
272
+ * @see [[setScalaTypeCacheSize ]]
273
+ * @since 2.14.3
274
+ */
275
+ def setLookupCacheFactory (lookupCacheFactory : LookupCacheFactory ): Unit = {
276
+ _lookupCacheFactory = lookupCacheFactory
277
+ recreateDescriptorCache()
278
+ recreateScalaTypeCache()
279
+ }
280
+
281
+ /**
282
+ * Resize the <code>descriptorCache</code>. The default size is 100.
283
+ * <p>
284
+ * Note that this clears the existing cache entries. It is best to set this up before you start using
285
+ * the Jackson Scala Module for serializing/deserializing.
286
+ * </p>
287
+ *
288
+ * @param size new size for the cache
289
+ * @see [[setScalaTypeCacheSize ]]
290
+ * @see [[setLookupCacheFactory ]]
291
+ * @since 2.14.3
292
+ */
293
+ def setDescriptorCacheSize (size : Int ): Unit = {
294
+ _descriptorCacheSize = size
295
+ recreateDescriptorCache()
296
+ }
297
+
298
+ /**
299
+ * Resize the <code>scalaTypeCache</code>. The default size is 1000.
300
+ * <p>
301
+ * Note that this clears the existing cache entries. It is best to set this up before you start using
302
+ * the Jackson Scala Module for serializing/deserializing.
303
+ * </p>
304
+ *
305
+ * @param size new size for the cache
306
+ * @see [[setDescriptorCacheSize ]]
307
+ * @see [[setLookupCacheFactory ]]
308
+ * @since 2.14.3
309
+ */
310
+ def setScalaTypeCacheSize (size : Int ): Unit = {
311
+ _scalaTypeCacheSize = size
312
+ recreateScalaTypeCache()
313
+ }
314
+
315
+ private def recreateDescriptorCache (): Unit = {
316
+ _descriptorCache.clear()
317
+ _descriptorCache = _lookupCacheFactory.createLookupCache(16 , _descriptorCacheSize)
318
+ }
319
+
320
+ private def recreateScalaTypeCache (): Unit = {
321
+ _scalaTypeCache.clear()
322
+ _scalaTypeCache = _lookupCacheFactory.createLookupCache(16 , _scalaTypeCacheSize)
323
+ }
259
324
260
325
/**
261
326
* jackson-module-scala does not always properly handle deserialization of Options or Collections wrapping
@@ -316,7 +381,7 @@ trait ScalaAnnotationIntrospectorModule extends JacksonModule {
316
381
}
317
382
318
383
/**
319
- * clears all the state associated with reference types
384
+ * Clears all the state associated with reference types
320
385
*
321
386
* @see [[registerReferencedValueType ]]
322
387
* @see [[clearRegisteredReferencedTypes(Class[_])]]
@@ -326,7 +391,17 @@ trait ScalaAnnotationIntrospectorModule extends JacksonModule {
326
391
overrideMap.clear()
327
392
}
328
393
329
- def setDescriptorCache (cache : LookupCache [ClassKey , BeanDescriptor ]): LookupCache [ClassKey , BeanDescriptor ] = {
394
+ /**
395
+ * Replace the <code>descriptorCache</code.
396
+ *
397
+ * @param cache new cache instance
398
+ * @return the existing cache instance
399
+ * @see [[setDescriptorCacheSize ]]
400
+ * @see [[setLookupCacheFactory ]]
401
+ * @deprecated key type will change to String in v2.15.0 and this function will be removed in a later release
402
+ */
403
+ @ deprecated(" key type will change to String in v2.15.0 and this function will be removed in a later release" , " 2.14.3" )
404
+ def setDescriptorCache (cache : LookupCache [String , BeanDescriptor ]): LookupCache [String , BeanDescriptor ] = {
330
405
val existingCache = _descriptorCache
331
406
_descriptorCache = cache
332
407
existingCache
@@ -336,10 +411,14 @@ trait ScalaAnnotationIntrospectorModule extends JacksonModule {
336
411
* Override the default <code>scalaTypeCache</code>.
337
412
*
338
413
* @param cache new cache instance
339
- * @return old cache instance
414
+ * @return existing cache instance
340
415
* @since 2.14.0
416
+ * @see [[setScalaTypeCacheSize ]]
417
+ * @see [[setLookupCacheFactory ]]
418
+ * @deprecated key type will change to String in v2.15.0 and this function will be removed in a later release
341
419
*/
342
- def setScalaTypeCache (cache : LookupCache [ClassKey , Boolean ]): LookupCache [ClassKey , Boolean ] = {
420
+ @ deprecated(" key type will change to String in v2.15.0 and this function will be removed in a later release" , " 2.14.3" )
421
+ def setScalaTypeCache (cache : LookupCache [String , Boolean ]): LookupCache [String , Boolean ] = {
343
422
val existingCache = _scalaTypeCache
344
423
_scalaTypeCache = cache
345
424
existingCache
0 commit comments