Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for value classes #693

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<groupId>com.github.wplong11</groupId>
<artifactId>jackson-databind</artifactId>
<version>revert-1005-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -253,5 +254,9 @@
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ internal class KotlinNamesAnnotationIntrospector(
if (member is Constructor<*>) {
val ctor = (member as Constructor<Any>)
val ctorParmCount = ctor.parameterTypes.size
val ktorParmCount = try { ctor.kotlinFunction?.parameters?.size ?: 0 }
val ktorParmCount = try { ctor.kotlinCtor?.parameters?.size ?: 0 }
catch (ex: KotlinReflectionInternalError) { 0 }
catch (ex: UnsupportedOperationException) { 0 }
if (ktorParmCount > 0 && ktorParmCount == ctorParmCount) {
ctor.kotlinFunction?.parameters?.get(param.index)?.name
ctor.kotlinCtor?.parameters?.get(param.index)?.name
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fasterxml.jackson.module.kotlin

import java.lang.reflect.Constructor
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.javaConstructor

val <T : Any> Constructor<T>.kotlinCtor: KFunction<T>?
get() {
val kotlinClass = declaringClass.kotlin
return if (kotlinClass.isValue) {
val parameterTypes = this.parameters.map { p -> p.type }
kotlinClass.constructors.firstOrNull { it.parameters.map { p -> p.type.erasedType() } == parameterTypes }
} else {
kotlinClass.constructors.firstOrNull { it.javaConstructor == this }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
LRUMap(0, reflectionCacheSize)

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

fun kotlinFromJava(key: Method): KFunction<*>? = javaMethodToKotlin.get(key)
?: key.kotlinFunction?.let { javaMethodToKotlin.putIfAbsent(key, it) ?: it }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fasterxml.jackson.module.kotlin.test

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
import org.junit.Assert
import org.junit.Test

class ValueClassTest {
@JvmInline
value class TestClass(val foo: List<Int>)

@Test
fun `test value class`() {
val mapper = createMapper()
Assert.assertEquals(listOf(1, 2), mapper.readValue("""{"foo": [1,2]}""", TestClass::class.java).foo)
wplong11 marked this conversation as resolved.
Show resolved Hide resolved
}

private fun createMapper(): ObjectMapper {
return ObjectMapper().registerModule(kotlinModule())
}
}