From ac7eba808f825aa5fc9c272e03a27a10ce3a9a1a Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 9 Jan 2021 12:59:17 +0900 Subject: [PATCH 1/5] Add configurations to use Java --- pom.xml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ca1e624c..94ecb0ca 100644 --- a/pom.xml +++ b/pom.xml @@ -114,7 +114,6 @@ - ${project.basedir}/src/main/kotlin ${project.basedir}/src/test/kotlin @@ -129,6 +128,12 @@ compile + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + From d23481c581a1e6b14bd24cf283168412a4bac2c3 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 9 Jan 2021 13:03:29 +0900 Subject: [PATCH 2/5] Add SpreadWrapper. --- .../fasterxml/jackson/module/kotlin/SpreadWrapper.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/fasterxml/jackson/module/kotlin/SpreadWrapper.java diff --git a/src/main/java/com/fasterxml/jackson/module/kotlin/SpreadWrapper.java b/src/main/java/com/fasterxml/jackson/module/kotlin/SpreadWrapper.java new file mode 100644 index 00000000..1f39fa53 --- /dev/null +++ b/src/main/java/com/fasterxml/jackson/module/kotlin/SpreadWrapper.java @@ -0,0 +1,10 @@ +package com.fasterxml.jackson.module.kotlin; + +import kotlin.reflect.KFunction; + +class SpreadWrapper { + // Wrapper to avoid costly calls using spread operator. + static T call(KFunction function, Object[] args) { + return function.call(args); + } +} From 25b3c79fd80a33d90ac552734fb70a4d2a5683fb Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 10 Jan 2021 14:43:27 +0900 Subject: [PATCH 3/5] Add argument bucket --- .../jackson/module/kotlin/ArgumentBucket.kt | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt new file mode 100644 index 00000000..b27422c7 --- /dev/null +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt @@ -0,0 +1,47 @@ +package com.fasterxml.jackson.module.kotlin + +import java.util.AbstractMap.SimpleEntry as Entry +import kotlin.reflect.KParameter + +internal class ArgumentBucket(private val parameters: List) : Map { + // This array is sorted by KParameter::index. + val valueArray: Array = arrayOfNulls(parameters.size) + private val initializationStatuses: BooleanArray = BooleanArray(parameters.size) + private var count = 0 + + operator fun set(key: KParameter, value: Any?): Any? { + return valueArray[key.index].apply { + valueArray[key.index] = value + + if (!initializationStatuses[key.index]) { + initializationStatuses[key.index] = true + count++ + } + } + } + + fun isFullInitialized(): Boolean = parameters.size == count + + override val entries: Set> + get() = valueArray.withIndex() + .filter { (i, _) -> initializationStatuses[i] } + .map { (i, arg) -> Entry(parameters[i], arg) } + .toSet() + override val keys: Set + get() = parameters + .filterIndexed { i, _ -> initializationStatuses[i] } + .toSet() + override val size: Int + get() = count + override val values: Collection + get() = valueArray.filterIndexed { i, _ -> initializationStatuses[i] } + + override fun containsKey(key: KParameter): Boolean = initializationStatuses[key.index] + + override fun containsValue(value: Any?): Boolean = valueArray.withIndex() + .any { (i, arg) -> initializationStatuses[i] && value == arg } + + override fun get(key: KParameter): Any? = valueArray[key.index] + + override fun isEmpty(): Boolean = count == 0 +} From 18ed9b2b44226f974699d7b140d8ae201fa40a25 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 9 Jan 2021 13:09:34 +0900 Subject: [PATCH 4/5] Use ArgumentBucket and SpreadWrapper --- .../module/kotlin/KotlinValueInstantiator.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt index ee55e949..27e80018 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -162,13 +162,18 @@ internal class KotlinValueInstantiator( ) { callable.isAccessible = true } - val callableParametersByName = linkedMapOf() - callableParameters.mapIndexed { idx, paramDef -> - if (paramDef != null) { - callableParametersByName[paramDef] = jsonParamValueList[idx] + ArgumentBucket(callable.parameters).apply { + callableParameters.forEachIndexed { idx, paramDef -> + if (paramDef != null) { + this[paramDef] = jsonParamValueList[idx] + } } + }.let { + if (it.isFullInitialized()) + SpreadWrapper.call(callable, it.valueArray) + else + callable.callBy(it) } - callable.callBy(callableParametersByName) } } From d26df02f8a8d0b87c1464ae4b35b9e31fb222139 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Fri, 7 May 2021 01:12:57 +0900 Subject: [PATCH 5/5] fix pom setting --- pom.xml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 94ecb0ca..5c432058 100644 --- a/pom.xml +++ b/pom.xml @@ -128,12 +128,6 @@ compile - - - ${project.basedir}/src/main/kotlin - ${project.basedir}/src/main/java - - @@ -172,6 +166,10 @@ true true + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java +