@@ -20,26 +20,30 @@ import io.github.projectmapk.jackson.module.kogera.ser.serializers.KotlinSeriali
2020import java.util.*
2121
2222/* *
23- * @param reflectionCacheSize Default: 512. Size, in items, of the caches used for mapping objects.
24- * @param nullToEmptyCollection Default: false. Whether to deserialize null values for collection properties as
25- * empty collections.
26- * @param nullToEmptyMap Default: false. Whether to deserialize null values for a map property to an empty
27- * map object.
28- * @param nullIsSameAsDefault Default false. Whether to treat null values as absent when deserializing, thereby
29- * using the default value provided in Kotlin.
30- * @param singletonSupport Default: DISABLED. Mode for singleton handling.
31- * See {@link io.github.projectmapk.jackson.module.kogera.SingletonSupport label}
32- * @param strictNullChecks Default: false. Whether to check deserialized collections. With this disabled,
33- * the default, collections which are typed to disallow null members
34- * (e.g. List<String>) may contain null values after deserialization. Enabling it
35- * protects against this but has significant performance impact.
36- * @param useJavaDurationConversion Default: false. Whether to use [java.time.Duration] as a bridge for [kotlin.time.Duration].
37- * This allows use Kotlin Duration type with [com.fasterxml.jackson.datatype.jsr310.JavaTimeModule].
23+ * @param initialCacheSize
24+ * Default: [Builder.DEFAULT_CACHE_SIZE]. See [Builder.withInitialCacheSize] for details.
25+ * @param maxCacheSize
26+ * Default: [Builder.DEFAULT_CACHE_SIZE]. See [Builder.withMaxCacheSize] for details.
27+ * @param nullToEmptyCollection
28+ * Default: false. See [KotlinFeature.NullToEmptyCollection] for details.
29+ * @param nullToEmptyMap
30+ * Default: false. See [KotlinFeature.NullToEmptyMap] for details.
31+ * @param nullIsSameAsDefault
32+ * Default: false. See [KotlinFeature.NullIsSameAsDefault] for details.
33+ * @param singletonSupport
34+ * Default: false. See [KotlinFeature.SingletonSupport] for details.
35+ * @param strictNullChecks
36+ * Default: false. See [KotlinFeature.StrictNullChecks] for details.
37+ * @param copySyntheticConstructorParameterAnnotations
38+ * Default false. See [KotlinFeature.CopySyntheticConstructorParameterAnnotations] for details.
39+ * @param useJavaDurationConversion
40+ * Default: false. See [KotlinFeature.UseJavaDurationConversion] for details.
3841 */
3942// Do not delete default arguments,
4043// as this will cause an error during initialization by Spring's Jackson2ObjectMapperBuilder.
4144public class KotlinModule private constructor(
42- public val reflectionCacheSize : Int = Builder .DEFAULT_CACHE_SIZE ,
45+ public val initialCacheSize : Int = Builder .DEFAULT_CACHE_SIZE ,
46+ public val maxCacheSize : Int = Builder .DEFAULT_CACHE_SIZE ,
4347 public val nullToEmptyCollection : Boolean = NullToEmptyCollection .enabledByDefault,
4448 public val nullToEmptyMap : Boolean = NullToEmptyMap .enabledByDefault,
4549 public val nullIsSameAsDefault : Boolean = NullIsSameAsDefault .enabledByDefault,
@@ -50,7 +54,8 @@ public class KotlinModule private constructor(
5054 public val useJavaDurationConversion : Boolean = UseJavaDurationConversion .enabledByDefault
5155) : SimpleModule(KotlinModule : :class.java.name, kogeraVersion) { // kogeraVersion is generated by building.
5256 private constructor (builder: Builder ) : this (
53- builder.reflectionCacheSize,
57+ builder.initialCacheSize,
58+ builder.maxCacheSize,
5459 builder.isEnabled(NullToEmptyCollection ),
5560 builder.isEnabled(NullToEmptyMap ),
5661 builder.isEnabled(NullIsSameAsDefault ),
@@ -66,8 +71,26 @@ public class KotlinModule private constructor(
6671 )
6772 public constructor () : this (Builder ())
6873
74+ init {
75+ checkMaxCacheSize(maxCacheSize)
76+ checkCacheSize(initialCacheSize, maxCacheSize)
77+ }
78+
6979 public companion object {
70- private const val serialVersionUID = 1L
80+ @Suppress(" ConstPropertyName" )
81+ private const val serialVersionUID = 2L
82+
83+ private fun checkMaxCacheSize (maxCacheSize : Int ) {
84+ if (maxCacheSize < 16 ) throw IllegalArgumentException (" 16 or higher must be specified" )
85+ }
86+
87+ private fun checkCacheSize (initialCacheSize : Int , maxCacheSize : Int ) {
88+ if (maxCacheSize < initialCacheSize) {
89+ throw IllegalArgumentException (
90+ " maxCacheSize($maxCacheSize ) was less than initialCacheSize($initialCacheSize )."
91+ )
92+ }
93+ }
7194 }
7295
7396 override fun setupModule (context : SetupContext ) {
@@ -79,7 +102,7 @@ public class KotlinModule private constructor(
79102 )
80103 }
81104
82- val cache = ReflectionCache (reflectionCacheSize )
105+ val cache = ReflectionCache (initialCacheSize, maxCacheSize )
83106
84107 context.addValueInstantiators(
85108 KotlinInstantiators (cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault)
@@ -115,13 +138,35 @@ public class KotlinModule private constructor(
115138 internal const val DEFAULT_CACHE_SIZE = 512
116139 }
117140
118- public var reflectionCacheSize: Int = DEFAULT_CACHE_SIZE
141+ public var initialCacheSize: Int = DEFAULT_CACHE_SIZE
142+ private set
143+ public var maxCacheSize: Int = DEFAULT_CACHE_SIZE
119144 private set
120145
121146 private val bitSet: BitSet = KotlinFeature .defaults
122147
123- public fun withReflectionCacheSize (reflectionCacheSize : Int ): Builder = apply {
124- this .reflectionCacheSize = reflectionCacheSize
148+ /* *
149+ * @throws IllegalArgumentException A value less than [maxCacheSize] was entered for [initialCacheSize].
150+ */
151+ public fun withInitialCacheSize (initialCacheSize : Int ): Builder = apply {
152+ checkCacheSize(initialCacheSize, maxCacheSize)
153+
154+ this .initialCacheSize = initialCacheSize
155+ }
156+
157+ /* *
158+ * Kogera's internal processing requires a certain cache size.
159+ * The lower limit of [maxCacheSize] is set to 16 for extreme use cases,
160+ * but it is recommended to set it to 100 or more unless there is a very clear reason.
161+ *
162+ * @throws IllegalArgumentException Specified size was less than the 16.
163+ * @throws IllegalArgumentException A value less than [initialCacheSize] was entered for maxCacheSize.
164+ */
165+ public fun withMaxCacheSize (maxCacheSize : Int ): Builder = apply {
166+ checkMaxCacheSize(maxCacheSize)
167+ checkCacheSize(initialCacheSize, maxCacheSize)
168+
169+ this .maxCacheSize = maxCacheSize
125170 }
126171
127172 public fun enable (feature : KotlinFeature ): Builder = apply {
0 commit comments