From 1b9a8fdcf43e4b59dee2654dec48a432353d541d Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 13 Sep 2020 19:46:21 +0900 Subject: [PATCH 01/11] =?UTF-8?q?=E3=83=97=E3=83=AA=E3=83=9F=E3=83=86?= =?UTF-8?q?=E3=82=A3=E3=83=96=E9=85=8D=E5=88=97=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/core/internal/ArgumentBucket.kt | 2 +- src/main/kotlin/com/mapk/core/internal/BucketGenerator.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/mapk/core/internal/ArgumentBucket.kt b/src/main/kotlin/com/mapk/core/internal/ArgumentBucket.kt index 26d3fc9..9f02a6d 100644 --- a/src/main/kotlin/com/mapk/core/internal/ArgumentBucket.kt +++ b/src/main/kotlin/com/mapk/core/internal/ArgumentBucket.kt @@ -7,7 +7,7 @@ import kotlin.reflect.KParameter internal class ArgumentBucket( private val keyList: List, val valueArray: Array, - initializationStatus: Array, + initializationStatus: BooleanArray, argumentBinders: List, adaptor: ArgumentAdaptor ) : Map { diff --git a/src/main/kotlin/com/mapk/core/internal/BucketGenerator.kt b/src/main/kotlin/com/mapk/core/internal/BucketGenerator.kt index 3356233..a629bb1 100644 --- a/src/main/kotlin/com/mapk/core/internal/BucketGenerator.kt +++ b/src/main/kotlin/com/mapk/core/internal/BucketGenerator.kt @@ -9,7 +9,7 @@ internal class BucketGenerator( instance: Any? ) { private val originalValueArray: Array = arrayOfNulls(parameters.size) - private val originalInitializationStatus: Array = Array(parameters.size) { false } + private val originalInitializationStatus: BooleanArray = BooleanArray(parameters.size) init { if (instance != null) { From 5331f304cc51adbfe3a8f2c55940da54bce25d0e Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 13 Sep 2020 20:02:08 +0900 Subject: [PATCH 02/11] update kotlin --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index b48580b..25de9ed 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ plugins { id("maven") id("java") - id("org.jetbrains.kotlin.jvm") version "1.4.0" + id("org.jetbrains.kotlin.jvm") version "1.4.10" id("org.jlleitschuh.gradle.ktlint") version "9.3.0" id("jacoco") } From 50f2dd0086aa403704f23957b984a38cc1e98ea2 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 13 Sep 2020 21:05:52 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=97?= =?UTF-8?q?=E5=AF=BE=E8=B1=A1=E3=81=8C=E3=82=B3=E3=83=B3=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=83=A9=E3=82=AF=E3=82=BF=E3=81=8B=E3=81=A4=E5=85=A8=E3=83=95?= =?UTF-8?q?=E3=82=A3=E3=83=BC=E3=83=AB=E3=83=89=E3=81=8C=E5=88=9D=E6=9C=9F?= =?UTF-8?q?=E5=8C=96=E3=81=A7=E3=81=8D=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=AE?= =?UTF-8?q?=E3=82=AA=E3=83=BC=E3=83=90=E3=83=BC=E3=83=98=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=82=92=E8=BB=BD=E6=B8=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/core/KFunctionForCall.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt index 4282305..6295d24 100644 --- a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt @@ -13,6 +13,7 @@ import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation import kotlin.reflect.jvm.isAccessible +import kotlin.reflect.jvm.javaConstructor class KFunctionForCall internal constructor( @TestOnly @@ -26,6 +27,10 @@ class KFunctionForCall internal constructor( instance ) + internal val fullInitializedFunctionLambda: (Array) -> T = function.javaConstructor?.let { + { values -> it.newInstance(*values) } + } ?: { function.call(*it) } + @TestOnly internal val parameters: List = function.parameters @@ -80,7 +85,7 @@ class KFunctionForCall internal constructor( fun call(adaptor: ArgumentAdaptor): T { val bucket = bucketGenerator.generate(adaptor) - return if (bucket.isInitialized) function.call(*bucket.valueArray) else function.callBy(bucket) + return if (bucket.isInitialized) fullInitializedFunctionLambda(bucket.valueArray) else function.callBy(bucket) } } From eb023dd6539bb25f5c4def39b0ba952bb86dd776 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 13 Sep 2020 21:06:06 +0900 Subject: [PATCH 04/11] =?UTF-8?q?=E3=83=90=E3=83=BC=E3=82=B8=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=82=A2=E3=83=83=E3=83=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 25de9ed..9c71270 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "com.mapk" -version = "0.17" +version = "0.18" java { sourceCompatibility = JavaVersion.VERSION_1_8 From 2658ac4e407b079cecfe845e3fecb54fd7b66ea3 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 13 Sep 2020 21:27:56 +0900 Subject: [PATCH 05/11] =?UTF-8?q?=E3=82=A2=E3=83=8E=E3=83=86=E3=83=BC?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AE=E4=BB=98=E3=81=91=E5=BF=98?= =?UTF-8?q?=E3=82=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/core/KFunctionForCall.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt index 6295d24..3db9e3a 100644 --- a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt @@ -27,6 +27,7 @@ class KFunctionForCall internal constructor( instance ) + @TestOnly internal val fullInitializedFunctionLambda: (Array) -> T = function.javaConstructor?.let { { values -> it.newInstance(*values) } } ?: { function.call(*it) } From e07ed132220aea78a98bc0c4f9923dad9671dd00 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 20 Sep 2020 19:09:08 +0900 Subject: [PATCH 06/11] =?UTF-8?q?=E5=AE=8C=E5=85=A8=E5=88=9D=E6=9C=9F?= =?UTF-8?q?=E5=8C=96=E6=99=82=E3=81=AE=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=97?= =?UTF-8?q?=E3=81=AE=E3=83=A9=E3=83=83=E3=83=91=E3=83=BC=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FullInitializedFunctionWrapper.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt diff --git a/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt b/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt new file mode 100644 index 0000000..8c64eef --- /dev/null +++ b/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt @@ -0,0 +1,29 @@ +package com.mapk.core.internal + +import kotlin.reflect.KFunction +import kotlin.reflect.jvm.javaConstructor +import kotlin.reflect.jvm.javaMethod + +internal class FullInitializedFunctionWrapper(function: KFunction, instance: Any?, paramSize: Int) { + private val lambda: (Array) -> T + + init { + val constructor = function.javaConstructor + + lambda = when { + constructor != null -> { + { constructor.newInstance(*it) } + } + instance != null -> { + val method = function.javaMethod!! + + @Suppress("UNCHECKED_CAST") { method.invoke(it[0], *(it.copyOfRange(1, paramSize))) as T } + } + else -> { + { function.call(*it) } + } + } + } + + fun call(args: Array): T = lambda(args) +} From 142c1470565fe2723073c2a152de1cf9925e020b Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 20 Sep 2020 19:09:25 +0900 Subject: [PATCH 07/11] =?UTF-8?q?=E3=83=A9=E3=83=83=E3=83=91=E3=83=BC?= =?UTF-8?q?=E3=82=92=E5=88=A9=E7=94=A8=E3=81=99=E3=82=8B=E5=BD=A2=E3=81=AB?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/core/KFunctionForCall.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt index 3db9e3a..9bd9a2b 100644 --- a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt @@ -3,6 +3,7 @@ package com.mapk.core import com.mapk.annotations.KParameterFlatten import com.mapk.core.internal.ArgumentBinder import com.mapk.core.internal.BucketGenerator +import com.mapk.core.internal.FullInitializedFunctionWrapper import com.mapk.core.internal.ParameterNameConverter import com.mapk.core.internal.getAliasOrName import com.mapk.core.internal.getKConstructor @@ -13,7 +14,6 @@ import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation import kotlin.reflect.jvm.isAccessible -import kotlin.reflect.jvm.javaConstructor class KFunctionForCall internal constructor( @TestOnly @@ -28,9 +28,7 @@ class KFunctionForCall internal constructor( ) @TestOnly - internal val fullInitializedFunctionLambda: (Array) -> T = function.javaConstructor?.let { - { values -> it.newInstance(*values) } - } ?: { function.call(*it) } + internal val fullInitializedWrapper: FullInitializedFunctionWrapper @TestOnly internal val parameters: List = function.parameters @@ -48,6 +46,8 @@ class KFunctionForCall internal constructor( // この関数には確実にアクセスするためアクセシビリティ書き換え function.isAccessible = true + fullInitializedWrapper = FullInitializedFunctionWrapper(function, instance, parameters.size) + val tempBinders = ArrayList() val tempList = ArrayList>() val tempMap = HashMap>() @@ -86,7 +86,7 @@ class KFunctionForCall internal constructor( fun call(adaptor: ArgumentAdaptor): T { val bucket = bucketGenerator.generate(adaptor) - return if (bucket.isInitialized) fullInitializedFunctionLambda(bucket.valueArray) else function.callBy(bucket) + return if (bucket.isInitialized) fullInitializedWrapper.call(bucket.valueArray) else function.callBy(bucket) } } From f4d3a6a7f552dbbd98920e95d016c28810aa7401 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 20 Sep 2020 19:34:38 +0900 Subject: [PATCH 08/11] =?UTF-8?q?=E5=BE=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mapk/core/internal/FullInitializedFunctionWrapper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt b/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt index 8c64eef..3f6369e 100644 --- a/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt +++ b/src/main/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapper.kt @@ -17,7 +17,7 @@ internal class FullInitializedFunctionWrapper(function: KFunction, instanc instance != null -> { val method = function.javaMethod!! - @Suppress("UNCHECKED_CAST") { method.invoke(it[0], *(it.copyOfRange(1, paramSize))) as T } + @Suppress("UNCHECKED_CAST") { method.invoke(instance, *(it.copyOfRange(1, paramSize))) as T } } else -> { { function.call(*it) } From b786b9c2aeb007b5ac85cbe08a0c6e9c29a627f4 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 20 Sep 2020 19:44:32 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FullInitializedFunctionWrapperTest.kt | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapperTest.kt diff --git a/src/test/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapperTest.kt b/src/test/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapperTest.kt new file mode 100644 index 0000000..b832344 --- /dev/null +++ b/src/test/kotlin/com/mapk/core/internal/FullInitializedFunctionWrapperTest.kt @@ -0,0 +1,56 @@ +package com.mapk.core.internal + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import kotlin.reflect.full.companionObject +import kotlin.reflect.full.companionObjectInstance +import kotlin.reflect.full.functions + +@DisplayName("完全初期化時呼び出しのテスト") +class FullInitializedFunctionWrapperTest { + data class Dst(val foo: Int, val bar: String) { + companion object { + fun of(foo: Int, bar: String) = Dst(foo, bar) + } + } + fun instanceMethod(foo: Int, bar: String) = Dst(foo, bar) + private val expected = Dst(1, "2") + + @Test + @DisplayName("コンストラクタの場合") + fun constructorTest() { + val fullInitializedFunctionWrapper = FullInitializedFunctionWrapper(::Dst, null, 2) + assertEquals(expected, fullInitializedFunctionWrapper.call(arrayOf(1, "2"))) + } + + @Test + @DisplayName("コンパニオンオブジェクトに定義した関数の場合") + fun companionObjectFunTest() { + val func = Dst::class.companionObject!!.functions.first { it.name == "of" } + val instance = Dst::class.companionObjectInstance!! + val fullInitializedFunctionWrapper = FullInitializedFunctionWrapper( + func, instance, 3 + ) + assertEquals(expected, fullInitializedFunctionWrapper.call(arrayOf(instance, 1, "2"))) + } + + @Nested + @DisplayName("その他の場合") + inner class OthersTest { + @Test + @DisplayName("コンパニオンオブジェクトに定義した関数をメソッドリファレンスで取得した場合") + fun companionObjectFunByMethodReferenceTest() { + val fullInitializedFunctionWrapper = FullInitializedFunctionWrapper((Dst)::of, null, 2) + assertEquals(expected, fullInitializedFunctionWrapper.call(arrayOf(1, "2"))) + } + + @Test + @DisplayName("インスタンスメソッドの場合") + fun instanceMethodTest() { + val fullInitializedFunctionWrapper = FullInitializedFunctionWrapper(::instanceMethod, null, 2) + assertEquals(expected, fullInitializedFunctionWrapper.call(arrayOf(1, "2"))) + } + } +} From c06b8afd70044739a645879f69a52fc385da46ee Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 20 Sep 2020 19:55:48 +0900 Subject: [PATCH 10/11] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/com/mapk/core/KParameterFlattenTest.kt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/kotlin/com/mapk/core/KParameterFlattenTest.kt b/src/test/kotlin/com/mapk/core/KParameterFlattenTest.kt index a3582a3..be3d0a3 100644 --- a/src/test/kotlin/com/mapk/core/KParameterFlattenTest.kt +++ b/src/test/kotlin/com/mapk/core/KParameterFlattenTest.kt @@ -2,8 +2,6 @@ package com.mapk.core import com.mapk.annotations.KConstructor import com.mapk.annotations.KParameterFlatten -import io.mockk.spyk -import io.mockk.verify import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.DisplayName @@ -44,8 +42,7 @@ class KParameterFlattenTest { @Test fun test() { - val spiedFunction = spyk(::Dst) - val function = KFunctionForCall(spiedFunction, { it }) + val function = KFunctionForCall(::Dst, { it }) function.requiredParameters.forEach { assertTrue(expectedParams.contains(it.name)) @@ -59,6 +56,5 @@ class KParameterFlattenTest { val actual = function.call(adaptor) assertEquals(expected, actual) - verify(exactly = 1) { spiedFunction.call(*anyVararg()) } } } From 5ce093cf3ad4ec5daa1bd272a5717ed544bf2827 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 20 Sep 2020 20:04:40 +0900 Subject: [PATCH 11/11] =?UTF-8?q?=E4=B8=80=E6=97=A6=E3=81=93=E3=81=93?= =?UTF-8?q?=E3=81=A7=E3=81=AF=E5=AE=8C=E5=85=A8=E5=88=9D=E6=9C=9F=E5=8C=96?= =?UTF-8?q?=E6=99=82=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=97=E3=82=92=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84=E5=BD=A2=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/com/mapk/core/KFunctionForCallTest.kt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/test/kotlin/com/mapk/core/KFunctionForCallTest.kt b/src/test/kotlin/com/mapk/core/KFunctionForCallTest.kt index 605d5f5..b324f02 100644 --- a/src/test/kotlin/com/mapk/core/KFunctionForCallTest.kt +++ b/src/test/kotlin/com/mapk/core/KFunctionForCallTest.kt @@ -51,7 +51,6 @@ class KFunctionForCallTest { fun fromCompanionObject() { val function = Companion::class.functions .first { it.name == (KFunctionForCallTest)::declaredOnCompanionObject.name } - .let { spyk(it) } val kFunctionForCall = KFunctionForCall(function, { it }, Companion) @@ -60,7 +59,6 @@ class KFunctionForCallTest { adaptor.putIfAbsent("arg2", 2) val result = kFunctionForCall.call(adaptor) assertEquals("12", result) - verify(exactly = 1) { function.call(*anyVararg()) } } private fun func(key: String, value: String = "default"): Pair = key to value @@ -84,7 +82,6 @@ class KFunctionForCallTest { fun multipleCall() { val function = Companion::class.functions .first { it.name == (KFunctionForCallTest)::declaredOnCompanionObject.name } - .let { spyk(it) } val kFunctionForCall = KFunctionForCall(function, { it }, Companion) @@ -101,8 +98,6 @@ class KFunctionForCallTest { .forEach { adaptor2.putIfAbsent(it.name!!, it.index + 1) } val result2 = kFunctionForCall.call(adaptor2) assertEquals("23", result2) - - verify(exactly = 2) { function.call(*anyVararg()) } } }