Skip to content

Commit 3a5331e

Browse files
committed
Fixed to use ValueClassBoxConverter
To reduce the amount of reflection during initialization.
1 parent 8924932 commit 3a5331e

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/Converters.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.fasterxml.jackson.databind.util.StdConverter
1111
*/
1212
internal class ValueClassBoxConverter<S : Any?, D : Any>(
1313
unboxedClass: Class<S>,
14-
valueClass: Class<D>
14+
val valueClass: Class<D>
1515
) : StdConverter<S, D>() {
1616
private val boxMethod = valueClass.getDeclaredMethod("box-impl", unboxedClass).apply {
1717
if (!this.isAccessible) this.isAccessible = true

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/deserializers/KotlinDeserializers.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
1212
import io.github.projectmapk.jackson.module.kogera.JmClass
1313
import io.github.projectmapk.jackson.module.kogera.KotlinDuration
1414
import io.github.projectmapk.jackson.module.kogera.ReflectionCache
15+
import io.github.projectmapk.jackson.module.kogera.ValueClassBoxConverter
1516
import io.github.projectmapk.jackson.module.kogera.deser.JavaToKotlinDurationConverter
1617
import io.github.projectmapk.jackson.module.kogera.hasCreatorAnnotation
1718
import io.github.projectmapk.jackson.module.kogera.isUnboxableValueClass
@@ -75,26 +76,23 @@ internal object ULongDeserializer : StdDeserializer<ULong>(ULong::class.java) {
7576
ULongChecker.readWithRangeCheck(p, p.bigIntegerValue)
7677
}
7778

78-
internal class ValueClassBoxDeserializer<T : Any>(
79+
internal class ValueClassBoxDeserializer<S, D : Any>(
7980
private val creator: Method,
80-
clazz: Class<T>
81-
) : StdDeserializer<T>(clazz) {
81+
private val converter: ValueClassBoxConverter<S, D>
82+
) : StdDeserializer<D>(converter.valueClass) {
8283
private val inputType: Class<*> = creator.parameterTypes[0]
83-
private val boxMethod: Method = clazz
84-
.getDeclaredMethod("box-impl", clazz.getDeclaredMethod("unbox-impl").returnType)
85-
.apply { if (!this.isAccessible) this.isAccessible = true }
8684

8785
init {
8886
creator.apply { if (!this.isAccessible) this.isAccessible = true }
8987
}
9088

91-
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): T {
89+
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): D {
9290
val input = p.readValueAs(inputType)
9391

9492
// To instantiate the value class in the same way as other classes,
9593
// it is necessary to call creator(e.g. constructor-impl) -> box-impl in that order.
9694
@Suppress("UNCHECKED_CAST")
97-
return boxMethod.invoke(null, creator.invoke(null, input)) as T
95+
return converter.convert(creator.invoke(null, input) as S)
9896
}
9997
}
10098

@@ -149,8 +147,11 @@ internal class KotlinDeserializers(
149147
rawClass == ULong::class.java -> ULongDeserializer
150148
rawClass == KotlinDuration::class.java ->
151149
JavaToKotlinDurationConverter.takeIf { useJavaDurationConversion }?.delegatingDeserializer
152-
rawClass.isUnboxableValueClass() -> findValueCreator(type, rawClass, cache.getJmClass(rawClass)!!)
153-
?.let { ValueClassBoxDeserializer(it, rawClass) }
150+
rawClass.isUnboxableValueClass() -> findValueCreator(type, rawClass, cache.getJmClass(rawClass)!!)?.let {
151+
val unboxedClass = rawClass.getMethod("unbox-impl").returnType
152+
val converter = cache.getValueClassBoxConverter(unboxedClass, rawClass)
153+
ValueClassBoxDeserializer(it, converter)
154+
}
154155
else -> null
155156
}
156157
}

0 commit comments

Comments
 (0)