Skip to content

Commit 8bb23d2

Browse files
committed
Support for value classes
resolve: FasterXML#199
1 parent 423fc83 commit 8bb23d2

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ internal class KotlinNamesAnnotationIntrospector(
127127
if (member is Constructor<*>) {
128128
val ctor = (member as Constructor<Any>)
129129
val ctorParmCount = ctor.parameterTypes.size
130-
val ktorParmCount = try { ctor.kotlinFunction?.parameters?.size ?: 0 }
130+
val ktorParmCount = try { ctor.kotlinCtor?.parameters?.size ?: 0 }
131131
catch (ex: KotlinReflectionInternalError) { 0 }
132132
catch (ex: UnsupportedOperationException) { 0 }
133133
if (ktorParmCount > 0 && ktorParmCount == ctorParmCount) {
134-
ctor.kotlinFunction?.parameters?.get(param.index)?.name
134+
ctor.kotlinCtor?.parameters?.get(param.index)?.name
135135
} else {
136136
null
137137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import java.lang.reflect.Constructor
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.jvm.javaConstructor
6+
7+
val <T : Any> Constructor<T>.kotlinCtor: KFunction<T>?
8+
get() {
9+
val kotlinClass = declaringClass.kotlin
10+
return if (kotlinClass.isValue) {
11+
val parameterTypes = this.parameters.map { p -> p.type }
12+
kotlinClass.constructors.firstOrNull { it.parameters.map { p -> p.type.erasedType() } == parameterTypes }
13+
} else {
14+
kotlinClass.constructors.firstOrNull { it.javaConstructor == this }
15+
}
16+
}

src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
5858
LRUMap(0, reflectionCacheSize)
5959

6060
fun kotlinFromJava(key: Constructor<Any>): KFunction<Any>? = javaConstructorToKotlin.get(key)
61-
?: key.kotlinFunction?.let { javaConstructorToKotlin.putIfAbsent(key, it) ?: it }
61+
?: key.kotlinCtor?.let { javaConstructorToKotlin.putIfAbsent(key, it) ?: it }
6262

6363
fun kotlinFromJava(key: Method): KFunction<*>? = javaMethodToKotlin.get(key)
6464
?: key.kotlinFunction?.let { javaMethodToKotlin.putIfAbsent(key, it) ?: it }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fasterxml.jackson.module.kotlin.test
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.module.kotlin.kotlinModule
5+
import org.junit.Assert
6+
import org.junit.Test
7+
8+
class ValueClassTest {
9+
@JvmInline
10+
value class TestClass(val foo: List<Int>)
11+
12+
@Test
13+
fun `test value class`() {
14+
val mapper = createMapper()
15+
Assert.assertEquals(listOf(1, 2), mapper.readValue("""{"foo": [1,2]}""", TestClass::class.java).foo)
16+
}
17+
18+
private fun createMapper(): ObjectMapper {
19+
return ObjectMapper().registerModule(kotlinModule())
20+
}
21+
}

0 commit comments

Comments
 (0)