@@ -3,6 +3,7 @@ package io.github.projectmapk.jackson.module.kogera
33import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
44import com.fasterxml.jackson.databind.util.LRUMap
55import java.io.Serializable
6+ import java.lang.reflect.Method
67import java.util.Optional
78
89internal class ReflectionCache (reflectionCacheSize : Int ) : Serializable {
@@ -16,7 +17,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
1617 private val classCache = LRUMap <Class <* >, JmClass > (reflectionCacheSize, reflectionCacheSize)
1718
1819 // Initial size is 0 because the value class is not always used
19- private val valueClassReturnTypeCache: LRUMap <AnnotatedMethod , Optional <Class <* >>> =
20+ private val valueClassReturnTypeCache: LRUMap <Method , Optional <Class <* >>> =
2021 LRUMap (0 , reflectionCacheSize)
2122
2223 // TODO: Consider whether the cache size should be reduced more,
@@ -46,28 +47,29 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
4647 }
4748 }
4849
49- private fun AnnotatedMethod.getValueClassReturnType (): Class <* >? {
50- val getter = this .member.apply {
51- // If the return value of the getter is a value class,
52- // it will be serialized properly without doing anything.
53- // TODO: Verify the case where a value class encompasses another value class.
54- if (this .returnType.isUnboxableValueClass()) return null
55- }
56- val kotlinProperty = getJmClass(getter.declaringClass)?.findPropertyByGetter(getter)
50+ private fun Method.getValueClassReturnType (): Class <* >? {
51+ val kotlinProperty = getJmClass(declaringClass)?.findPropertyByGetter(this )
5752
5853 // Since there was no way to directly determine whether returnType is a value class or not,
5954 // Class is restored and processed.
6055 return kotlinProperty?.returnType?.reconstructClassOrNull()?.takeIf { it.isUnboxableValueClass() }
6156 }
6257
63- fun findValueClassReturnType (getter : AnnotatedMethod ): Class <* >? {
64- val optional = valueClassReturnTypeCache.get(getter)
58+ // Return boxed type on Kotlin for unboxed getters
59+ fun findBoxedReturnType (getter : AnnotatedMethod ): Class <* >? {
60+ val method = getter.member
61+ val optional = valueClassReturnTypeCache.get(method)
6562
6663 return if (optional != null ) {
6764 optional
6865 } else {
69- val value = Optional .ofNullable(getter.getValueClassReturnType())
70- (valueClassReturnTypeCache.putIfAbsent(getter, value) ? : value)
66+ // If the return value of the getter is a value class,
67+ // it will be serialized properly without doing anything.
68+ // TODO: Verify the case where a value class encompasses another value class.
69+ if (method.returnType.isUnboxableValueClass()) return null
70+
71+ val value = Optional .ofNullable(method.getValueClassReturnType())
72+ (valueClassReturnTypeCache.putIfAbsent(method, value) ? : value)
7173 }.orElse(null )
7274 }
7375
0 commit comments