From f3dd759c68b7d0ec525702014749ce6165be4b5d Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 14 Jun 2020 04:54:07 +0900 Subject: [PATCH 1/3] =?UTF-8?q?KConstructor=E3=81=AE=E5=8F=96=E3=82=8A?= =?UTF-8?q?=E5=87=BA=E3=81=97=E3=82=92=E5=88=87=E3=82=8A=E5=87=BA=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/core/Functions.kt | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/kotlin/com/mapk/core/Functions.kt b/src/main/kotlin/com/mapk/core/Functions.kt index 7ef037e..cb5a60e 100644 --- a/src/main/kotlin/com/mapk/core/Functions.kt +++ b/src/main/kotlin/com/mapk/core/Functions.kt @@ -1,10 +1,13 @@ package com.mapk.core +import com.mapk.annotations.KConstructor import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.companionObject import kotlin.reflect.full.functions +import kotlin.reflect.full.primaryConstructor +import kotlin.reflect.jvm.jvmName inline fun KClass<*>.getAnnotatedFunctionsFromCompanionObject(): Pair>>? { return this.companionObject?.let { companionObject -> @@ -23,4 +26,25 @@ inline fun Collection>.getAnnotatedFunc return filter { function -> function.annotations.any { it is A } } } +@Suppress("UNCHECKED_CAST") +fun KClass.getKConstructor(): Pair> { + val constructors = ArrayList>>() + + this.getAnnotatedFunctionsFromCompanionObject()?.let { (instance, functions) -> + functions.forEach { + constructors.add(instance to it as KFunction) + } + } + + this.constructors.getAnnotatedFunctions().forEach { + constructors.add(null to it) + } + + if (constructors.size == 1) return constructors.single() + + if (constructors.isEmpty()) return null to this.primaryConstructor!! + + throw IllegalArgumentException("${this.jvmName} has multiple ${KConstructor::class.jvmName}.") +} + fun KParameter.getKClass(): KClass<*> = type.classifier as KClass<*> From 16e6cf9a4f945d8c345d8eae3fa1b0270a3b7075 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 14 Jun 2020 04:54:18 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=87=E3=82=8A=E5=87=BA=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=82=82=E3=81=AE=E3=82=92=E4=BD=BF=E3=81=86=E3=82=88?= =?UTF-8?q?=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/mapk/core/KFunctionForCall.kt | 33 ++++--------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt index 3be3f43..70a74a0 100644 --- a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt @@ -1,6 +1,5 @@ package com.mapk.core -import com.mapk.annotations.KConstructor import com.mapk.annotations.KParameterFlatten import com.mapk.core.internal.ArgumentBinder import com.mapk.core.internal.BucketGenerator @@ -11,9 +10,7 @@ import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation -import kotlin.reflect.full.primaryConstructor import kotlin.reflect.jvm.isAccessible -import kotlin.reflect.jvm.jvmName import org.jetbrains.annotations.TestOnly class KFunctionForCall internal constructor( @@ -86,30 +83,10 @@ class KFunctionForCall internal constructor( } } -@Suppress("UNCHECKED_CAST") -internal fun KClass.toKConstructor(parameterNameConverter: ParameterNameConverter): KFunctionForCall { - val constructors = ArrayList>() - - this.getAnnotatedFunctionsFromCompanionObject()?.let { (instance, functions) -> - functions.forEach { - constructors.add(KFunctionForCall(it as KFunction, parameterNameConverter, instance)) - } - } - - this.constructors.getAnnotatedFunctions().forEach { - constructors.add(KFunctionForCall(it, parameterNameConverter)) - } - - if (constructors.size == 1) return constructors.single() - - if (constructors.isEmpty()) return KFunctionForCall(this.primaryConstructor!!, parameterNameConverter) - - throw IllegalArgumentException("${this.jvmName} has multiple ${KConstructor::class.jvmName}.") -} - -@Suppress("UNCHECKED_CAST") fun KClass.toKConstructor(parameterNameConverter: ((String) -> String)?): KFunctionForCall = - this.toKConstructor(ParameterNameConverter.Simple(parameterNameConverter)) + this.getKConstructor().let { (instance, function) -> + KFunctionForCall(function, ParameterNameConverter.Simple(parameterNameConverter), instance) + } private fun KParameter.toArgumentBinder(parameterNameConverter: ParameterNameConverter): ArgumentBinder { val name = getAliasOrName()!! @@ -124,7 +101,9 @@ private fun KParameter.toArgumentBinder(parameterNameConverter: ParameterNameCon parameterNameConverter.toSimple() } - ArgumentBinder.Function(getKClass().toKConstructor(converter), index, annotations) + getKClass().getKConstructor().let { (instance, function) -> + ArgumentBinder.Function(KFunctionForCall(function, converter, instance), index, annotations) + } } ?: ArgumentBinder.Value( index, annotations, From d4c4c54e25948ec0389af66c95b014a641f61413 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 14 Jun 2020 04:55:50 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E5=A0=B4=E6=89=80?= =?UTF-8?q?=E3=81=AE=E9=96=93=E9=81=95=E3=81=84=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 合わせてinternal化 --- src/main/kotlin/com/mapk/core/Functions.kt | 24 ----------------- .../kotlin/com/mapk/core/KFunctionForCall.kt | 1 + .../com/mapk/core/internal/Functions.kt | 27 +++++++++++++++++++ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/mapk/core/Functions.kt b/src/main/kotlin/com/mapk/core/Functions.kt index cb5a60e..7ef037e 100644 --- a/src/main/kotlin/com/mapk/core/Functions.kt +++ b/src/main/kotlin/com/mapk/core/Functions.kt @@ -1,13 +1,10 @@ package com.mapk.core -import com.mapk.annotations.KConstructor import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.companionObject import kotlin.reflect.full.functions -import kotlin.reflect.full.primaryConstructor -import kotlin.reflect.jvm.jvmName inline fun KClass<*>.getAnnotatedFunctionsFromCompanionObject(): Pair>>? { return this.companionObject?.let { companionObject -> @@ -26,25 +23,4 @@ inline fun Collection>.getAnnotatedFunc return filter { function -> function.annotations.any { it is A } } } -@Suppress("UNCHECKED_CAST") -fun KClass.getKConstructor(): Pair> { - val constructors = ArrayList>>() - - this.getAnnotatedFunctionsFromCompanionObject()?.let { (instance, functions) -> - functions.forEach { - constructors.add(instance to it as KFunction) - } - } - - this.constructors.getAnnotatedFunctions().forEach { - constructors.add(null to it) - } - - if (constructors.size == 1) return constructors.single() - - if (constructors.isEmpty()) return null to this.primaryConstructor!! - - throw IllegalArgumentException("${this.jvmName} has multiple ${KConstructor::class.jvmName}.") -} - fun KParameter.getKClass(): KClass<*> = type.classifier as KClass<*> diff --git a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt index 70a74a0..7586e05 100644 --- a/src/main/kotlin/com/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/mapk/core/KFunctionForCall.kt @@ -5,6 +5,7 @@ import com.mapk.core.internal.ArgumentBinder import com.mapk.core.internal.BucketGenerator import com.mapk.core.internal.ParameterNameConverter import com.mapk.core.internal.getAliasOrName +import com.mapk.core.internal.getKConstructor import com.mapk.core.internal.isUseDefaultArgument import kotlin.reflect.KClass import kotlin.reflect.KFunction diff --git a/src/main/kotlin/com/mapk/core/internal/Functions.kt b/src/main/kotlin/com/mapk/core/internal/Functions.kt index 47bf781..d8bbd8a 100644 --- a/src/main/kotlin/com/mapk/core/internal/Functions.kt +++ b/src/main/kotlin/com/mapk/core/internal/Functions.kt @@ -1,10 +1,16 @@ package com.mapk.core.internal +import com.mapk.annotations.KConstructor import com.mapk.annotations.KParameterAlias import com.mapk.annotations.KUseDefaultArgument +import com.mapk.core.getAnnotatedFunctions +import com.mapk.core.getAnnotatedFunctionsFromCompanionObject import java.lang.IllegalArgumentException +import kotlin.reflect.KClass +import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation +import kotlin.reflect.full.primaryConstructor import kotlin.reflect.jvm.jvmName /** @@ -24,3 +30,24 @@ internal fun KParameter.isUseDefaultArgument(): Boolean { } return false } + +@Suppress("UNCHECKED_CAST") +internal fun KClass.getKConstructor(): Pair> { + val constructors = ArrayList>>() + + this.getAnnotatedFunctionsFromCompanionObject()?.let { (instance, functions) -> + functions.forEach { + constructors.add(instance to it as KFunction) + } + } + + this.constructors.getAnnotatedFunctions().forEach { + constructors.add(null to it) + } + + if (constructors.size == 1) return constructors.single() + + if (constructors.isEmpty()) return null to this.primaryConstructor!! + + throw IllegalArgumentException("${this.jvmName} has multiple ${KConstructor::class.jvmName}.") +}