From 9660bfd5a4fa4c05c76c95626b330e6e917a682d Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 21:53:16 +0900 Subject: [PATCH 01/27] =?UTF-8?q?findAnnotation=E3=82=92=E4=BD=BF=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E6=9B=B8=E3=81=8D=E7=9B=B4=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 11 +++-------- .../com/wrongwrong/mapk/core/ParameterForMap.kt | 6 ++---- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 1083db6..901e4b1 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -8,6 +8,7 @@ import kotlin.reflect.KFunction import kotlin.reflect.KProperty1 import kotlin.reflect.KVisibility import kotlin.reflect.full.companionObjectInstance +import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.functions import kotlin.reflect.full.isSuperclassOf import kotlin.reflect.full.memberProperties @@ -50,10 +51,7 @@ class KMapper(private val function: KFunction, propertyNameConverter src::class.memberProperties.filterTargets().associate { property -> val getter = property.getAccessibleGetter() - val key = getter.annotations - .find { it is KPropertyAlias } - ?.let { (it as KPropertyAlias).value } - ?: property.name + val key = getter.findAnnotation()?.value ?: property.name key to getter } @@ -78,10 +76,7 @@ class KMapper(private val function: KFunction, propertyNameConverter arg::class.memberProperties.filterTargets().associate { property -> val getter = property.getAccessibleGetter() - val key = getter.annotations - .find { it is KPropertyAlias } - ?.let { (it as KPropertyAlias).value } - ?: property.name + val key = getter.findAnnotation()?.value ?: property.name key to { getter.call(arg) } } diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt index 8df3d96..9f90d1a 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt @@ -6,6 +6,7 @@ import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.companionObjectInstance +import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.functions import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.staticFunctions @@ -16,10 +17,7 @@ internal class ParameterForMap private constructor( val clazz: KClass, propertyNameConverter: (String) -> String ) { - val name: String = param.annotations - .find { it is KPropertyAlias } - ?.let { (it as KPropertyAlias).value } - ?: propertyNameConverter(param.name!!) + val name: String = param.findAnnotation()?.value ?: propertyNameConverter(param.name!!) val javaClazz: Class by lazy { clazz.java From 0e6a8956f30deaa7c4c0bea9ce3325adc9ab9758 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 22:12:37 +0900 Subject: [PATCH 02/27] =?UTF-8?q?=E3=82=B7=E3=83=B3=E3=83=97=E3=83=AB?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 901e4b1..44daf80 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -51,9 +51,7 @@ class KMapper(private val function: KFunction, propertyNameConverter src::class.memberProperties.filterTargets().associate { property -> val getter = property.getAccessibleGetter() - val key = getter.findAnnotation()?.value ?: property.name - - key to getter + (getter.findAnnotation()?.value ?: property.name) to getter } return parameters.associate { @@ -76,9 +74,7 @@ class KMapper(private val function: KFunction, propertyNameConverter arg::class.memberProperties.filterTargets().associate { property -> val getter = property.getAccessibleGetter() - val key = getter.findAnnotation()?.value ?: property.name - - key to { getter.call(arg) } + (getter.findAnnotation()?.value ?: property.name) to { getter.call(arg) } } } } From 10c0c6ab64c500798083f62d45356ef4806e6210 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:00:32 +0900 Subject: [PATCH 03/27] =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=81=AEmap=EF=BC=88=E4=BB=AE=E7=BD=AE=E3=81=8D?= =?UTF-8?q?=EF=BC=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 44daf80..b7a980e 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -24,6 +24,12 @@ class KMapper(private val function: KFunction, propertyNameConverter .map { ParameterForMap.newInstance(it, propertyNameConverter) } .toSet() + private val parameterMap: Map> = function.parameters + .associate { + val param = ParameterForMap.newInstance(it, propertyNameConverter) + param.name to param + } + init { if (parameters.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") From 108374cdef56a9e0a79557b4cd6606a955d4dfd1 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:03:52 +0900 Subject: [PATCH 04/27] =?UTF-8?q?parameterMap=E3=82=92=E7=94=A8=E3=81=84?= =?UTF-8?q?=E3=82=8B=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/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index b7a980e..3bf93b6 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -38,12 +38,12 @@ class KMapper(private val function: KFunction, propertyNameConverter } fun map(srcMap: Map): T { - return parameters.associate { - // 取得した内容がnullでなければ適切にmapする - it.param to srcMap.getValue(it.name)?.let { value -> - mapObject(it, value) + return srcMap.entries.mapNotNull { (key, value) -> + parameterMap[key]?.let { param -> + // 取得した内容がnullでなければ適切にmapする + param.param to value?.let { mapObject(param, it) } } - }.let { function.callBy(it) } + }.let { function.callBy(it.toMap()) } } fun map(srcPair: Pair): T = parameters From 3cd5cb14ac2ff19ca172fa0f9abc18aaca97f31e Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:06:32 +0900 Subject: [PATCH 05/27] =?UTF-8?q?parameterMap=E3=82=92=E7=94=A8=E3=81=84?= =?UTF-8?q?=E3=82=8B=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/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 3bf93b6..2f1965b 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -46,11 +46,9 @@ class KMapper(private val function: KFunction, propertyNameConverter }.let { function.callBy(it.toMap()) } } - fun map(srcPair: Pair): T = parameters - .single { it.name == srcPair.first } - .let { - function.callBy(mapOf(it.param to srcPair.second?.let { value -> mapObject(it, value) })) - } + fun map(srcPair: Pair): T = parameterMap.getValue(srcPair.first).let { + function.callBy(mapOf(it.param to srcPair.second?.let { value -> mapObject(it, value) })) + } fun map(src: Any): T { val srcMap: Map> = From 1441f238eaa248c149737c1b28e0b8b35db4491a Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:25:04 +0900 Subject: [PATCH 06/27] =?UTF-8?q?parameterMap=E3=82=92=E7=94=A8=E3=81=84?= =?UTF-8?q?=E3=82=8B=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 --- .../com/wrongwrong/mapk/core/KMapper.kt | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 2f1965b..927fc95 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -50,21 +50,12 @@ class KMapper(private val function: KFunction, propertyNameConverter function.callBy(mapOf(it.param to srcPair.second?.let { value -> mapObject(it, value) })) } - fun map(src: Any): T { - val srcMap: Map> = - src::class.memberProperties.filterTargets().associate { property -> - val getter = property.getAccessibleGetter() - - (getter.findAnnotation()?.value ?: property.name) to getter - } - - return parameters.associate { - // 取得した内容がnullでなければ適切にmapする - it.param to srcMap.getValue(it.name).call(src)?.let { value -> - mapObject(it, value) - } - }.let { function.callBy(it) } - } + fun map(src: Any): T = src::class.memberProperties.filterTargets().mapNotNull { property -> + val getter = property.getAccessibleGetter() + parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { + it.param to getter.call(src)?.let { value -> mapObject(it, value) } + } + }.let { function.callBy(it.toMap()) } fun map(vararg args: Any): T { val srcMap: Map Any?> = listOf(*args) From b4d7035d997170f6d22735ae421c71f931336a0d Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:26:42 +0900 Subject: [PATCH 07/27] =?UTF-8?q?parameterMap=E3=82=92=E7=94=A8=E3=81=84?= =?UTF-8?q?=E3=82=8B=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 --- .../com/wrongwrong/mapk/core/KMapper.kt | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 927fc95..1456645 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -57,33 +57,25 @@ class KMapper(private val function: KFunction, propertyNameConverter } }.let { function.callBy(it.toMap()) } - fun map(vararg args: Any): T { - val srcMap: Map Any?> = listOf(*args) - .map { arg -> - when (arg) { - is Map<*, *> -> arg.entries.associate { (key, value) -> - (key as String) to { value } - } - is Pair<*, *> -> mapOf(arg.first as String to { arg.second }) - else -> { - arg::class.memberProperties.filterTargets().associate { property -> - val getter = property.getAccessibleGetter() - - (getter.findAnnotation()?.value ?: property.name) to { getter.call(arg) } - } - } + fun map(vararg args: Any): T = listOf(*args).map { arg -> + when (arg) { + is Map<*, *> -> arg.entries.mapNotNull { (key, value) -> + parameterMap[key]?.let { param -> + param.param to value?.let { mapObject(param, it) } } - }.reduce { acc, map -> - acc + map } - - return parameters.associate { - // 取得した内容がnullでなければ適切にmapする - it.param to srcMap.getValue(it.name)()?.let { value -> - mapObject(it, value) + is Pair<*, *> -> { + val param = parameterMap.getValue(arg.first as String) + listOf(param.param to arg.second?.let { mapObject(param, it) }) } - }.let { function.callBy(it) } - } + else -> arg::class.memberProperties.filterTargets().mapNotNull { property -> + val getter = property.getAccessibleGetter() + parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { + it.param to getter.call(arg)?.let { value -> mapObject(it, value) } + } + } + } + }.flatten().let { function.callBy(it.toMap()) } } private fun Collection>.filterTargets(): Collection> { From e1f317966be5267f029be3f4718adb6a93dd2ab5 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:27:52 +0900 Subject: [PATCH 08/27] =?UTF-8?q?=E5=88=A9=E7=94=A8=E3=81=97=E3=81=AA?= =?UTF-8?q?=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=9F=E3=81=9F=E3=82=81=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 1456645..9c92e3f 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -20,10 +20,6 @@ class KMapper(private val function: KFunction, propertyNameConverter getTarget(clazz), propertyNameConverter ) - private val parameters: Set> = function.parameters - .map { ParameterForMap.newInstance(it, propertyNameConverter) } - .toSet() - private val parameterMap: Map> = function.parameters .associate { val param = ParameterForMap.newInstance(it, propertyNameConverter) @@ -31,7 +27,7 @@ class KMapper(private val function: KFunction, propertyNameConverter } init { - if (parameters.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") + if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") // private関数に対してもマッピングできなければ何かと不都合があるため、accessibleは書き換える function.isAccessible = true From 3c809d1669168c746941f8c8d6c68bedc386feb1 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:32:08 +0900 Subject: [PATCH 09/27] =?UTF-8?q?name=E3=81=AFmap=E3=81=AB=E7=BD=AE?= =?UTF-8?q?=E3=81=8F=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/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 4 ++-- .../com/wrongwrong/mapk/core/ParameterForMap.kt | 11 +++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 9c92e3f..e2cfd26 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -22,8 +22,8 @@ class KMapper(private val function: KFunction, propertyNameConverter private val parameterMap: Map> = function.parameters .associate { - val param = ParameterForMap.newInstance(it, propertyNameConverter) - param.name to param + (it.findAnnotation()?.value ?: propertyNameConverter(it.name!!)) to + ParameterForMap.newInstance(it) } init { diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt index 9f90d1a..a83e1f8 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt @@ -1,12 +1,10 @@ package com.wrongwrong.mapk.core import com.wrongwrong.mapk.annotations.KConverter -import com.wrongwrong.mapk.annotations.KPropertyAlias import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.companionObjectInstance -import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.functions import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.staticFunctions @@ -14,11 +12,8 @@ import kotlin.reflect.jvm.isAccessible internal class ParameterForMap private constructor( val param: KParameter, - val clazz: KClass, - propertyNameConverter: (String) -> String + val clazz: KClass ) { - val name: String = param.findAnnotation()?.value ?: propertyNameConverter(param.name!!) - val javaClazz: Class by lazy { clazz.java } @@ -32,8 +27,8 @@ internal class ParameterForMap private constructor( creators.find { (key, _) -> input.isSubclassOf(key) }?.second companion object { - fun newInstance(param: KParameter, propertyNameConverter: (String) -> String): ParameterForMap<*> { - return ParameterForMap(param, param.type.classifier as KClass<*>, propertyNameConverter) + fun newInstance(param: KParameter): ParameterForMap<*> { + return ParameterForMap(param, param.type.classifier as KClass<*>) } } } From c5112f1a9efd51bd3cfea225ec39c4300e29a58a Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 22 Feb 2020 23:33:34 +0900 Subject: [PATCH 10/27] =?UTF-8?q?=E6=95=B4=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 9 ++++----- .../kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt | 5 +---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index e2cfd26..eceab81 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -20,11 +20,10 @@ class KMapper(private val function: KFunction, propertyNameConverter getTarget(clazz), propertyNameConverter ) - private val parameterMap: Map> = function.parameters - .associate { - (it.findAnnotation()?.value ?: propertyNameConverter(it.name!!)) to - ParameterForMap.newInstance(it) - } + private val parameterMap: Map> = function.parameters.associate { + (it.findAnnotation()?.value ?: propertyNameConverter(it.name!!)) to + ParameterForMap.newInstance(it) + } init { if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt index a83e1f8..5345716 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt @@ -10,10 +10,7 @@ import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.staticFunctions import kotlin.reflect.jvm.isAccessible -internal class ParameterForMap private constructor( - val param: KParameter, - val clazz: KClass -) { +internal class ParameterForMap private constructor(val param: KParameter, val clazz: KClass) { val javaClazz: Class by lazy { clazz.java } From 535a190ca4d32bf0cbf7ae8f621cd5d08b89822e Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 04:06:53 +0900 Subject: [PATCH 11/27] =?UTF-8?q?=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=97?= =?UTF-8?q?=E9=AB=98=E9=80=9F=E5=8C=96=E7=94=A8=E3=81=AB=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=B9=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wrongwrong/mapk/core/KFunctionForCall.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt new file mode 100644 index 0000000..ec8a8a9 --- /dev/null +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt @@ -0,0 +1,23 @@ +package com.wrongwrong.mapk.core + +import kotlin.reflect.KFunction +import kotlin.reflect.jvm.isAccessible + +class KFunctionForCall( + private val function: KFunction, + parameterSize: Int, + instance: Any? = null +) { + private val originalArray: Array + val argumentArray: Array get() = originalArray.copyOf() + + init { + // この関数には確実にアクセスするためアクセシビリティ書き換え + function.isAccessible = true + + // 必要が有ればinstanceを先に、無ければ + originalArray = Array(parameterSize) { if (it == 0 && instance != null) instance else null } + } + + fun call(arguments: Array): T = function.call(*arguments) +} From 7decb7a4cd63d73fe97182077b6e5ae62ed45a04 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 04:07:59 +0900 Subject: [PATCH 12/27] =?UTF-8?q?call=E3=81=AB=E5=88=87=E3=82=8A=E6=9B=BF?= =?UTF-8?q?=E3=81=88=E3=81=A6=E9=AB=98=E9=80=9F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wrongwrong/mapk/core/KMapper.kt | 90 ++++++++++++------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index eceab81..9b67c17 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -5,6 +5,7 @@ import com.wrongwrong.mapk.annotations.KPropertyAlias import com.wrongwrong.mapk.annotations.KPropertyIgnore import kotlin.reflect.KClass import kotlin.reflect.KFunction +import kotlin.reflect.KParameter import kotlin.reflect.KProperty1 import kotlin.reflect.KVisibility import kotlin.reflect.full.companionObjectInstance @@ -15,62 +16,87 @@ import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor import kotlin.reflect.jvm.isAccessible -class KMapper(private val function: KFunction, propertyNameConverter: (String) -> String = { it }) { +class KMapper(function: KFunction, propertyNameConverter: (String) -> String = { it }) { constructor(clazz: KClass, propertyNameConverter: (String) -> String = { it }) : this( getTarget(clazz), propertyNameConverter ) - private val parameterMap: Map> = function.parameters.associate { - (it.findAnnotation()?.value ?: propertyNameConverter(it.name!!)) to - ParameterForMap.newInstance(it) - } + private val parameterMap: Map> + private val functionForCall: KFunctionForCall init { - if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") + val params = function.parameters + functionForCall = KFunctionForCall(function, params.size) // TODO: 必要に応じてインスタンスを渡す + + parameterMap = params.filter { it.kind != KParameter.Kind.INSTANCE }.associate { + (it.findAnnotation()?.value ?: propertyNameConverter(it.name!!)) to + ParameterForMap.newInstance(it) + } - // private関数に対してもマッピングできなければ何かと不都合があるため、accessibleは書き換える - function.isAccessible = true + if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") } fun map(srcMap: Map): T { - return srcMap.entries.mapNotNull { (key, value) -> + val array: Array = functionForCall.argumentArray + + srcMap.forEach { (key, value) -> parameterMap[key]?.let { param -> // 取得した内容がnullでなければ適切にmapする - param.param to value?.let { mapObject(param, it) } + array[param.param.index] = value?.let { mapObject(param, it) } } - }.let { function.callBy(it.toMap()) } + } + + return functionForCall.call(array) } fun map(srcPair: Pair): T = parameterMap.getValue(srcPair.first).let { - function.callBy(mapOf(it.param to srcPair.second?.let { value -> mapObject(it, value) })) + val array: Array = functionForCall.argumentArray + array[it.param.index] = srcPair.second?.let { value -> mapObject(it, value) } + functionForCall.call(array) } - fun map(src: Any): T = src::class.memberProperties.filterTargets().mapNotNull { property -> - val getter = property.getAccessibleGetter() - parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { - it.param to getter.call(src)?.let { value -> mapObject(it, value) } - } - }.let { function.callBy(it.toMap()) } + fun map(src: Any): T { + val array: Array = functionForCall.argumentArray - fun map(vararg args: Any): T = listOf(*args).map { arg -> - when (arg) { - is Map<*, *> -> arg.entries.mapNotNull { (key, value) -> - parameterMap[key]?.let { param -> - param.param to value?.let { mapObject(param, it) } - } - } - is Pair<*, *> -> { - val param = parameterMap.getValue(arg.first as String) - listOf(param.param to arg.second?.let { mapObject(param, it) }) - } - else -> arg::class.memberProperties.filterTargets().mapNotNull { property -> + src::class.memberProperties.forEach { property -> + if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { val getter = property.getAccessibleGetter() parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { - it.param to getter.call(arg)?.let { value -> mapObject(it, value) } + array[it.param.index] = getter.call(src)?.let { value -> mapObject(it, value) } } } } - }.flatten().let { function.callBy(it.toMap()) } + + return functionForCall.call(array) + } + + fun map(vararg args: Any): T { + val array: Array = functionForCall.argumentArray + + listOf(*args).forEach { arg -> + when (arg) { + is Map<*, *> -> arg.forEach { (key, value) -> + parameterMap[key]?.let { param -> + // 取得した内容がnullでなければ適切にmapする + array[param.param.index] = value?.let { mapObject(param, it) } + } + } + is Pair<*, *> -> parameterMap.getValue(arg.first as String).let { + array[it.param.index] = arg.second?.let { value -> mapObject(it, value) } + } + else -> arg::class.memberProperties.forEach { property -> + if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { + val getter = property.getAccessibleGetter() + parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { + array[it.param.index] = getter.call(arg)?.let { value -> mapObject(it, value) } + } + } + } + } + } + + return functionForCall.call(array) + } } private fun Collection>.filterTargets(): Collection> { From b1bdaccf67bd610ee01e9e7db898bf336bac2da4 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 11:52:03 +0900 Subject: [PATCH 13/27] =?UTF-8?q?=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=AE=E5=8F=96=E3=82=8A=E6=96=B9?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3=E3=81=97=E8=8B=A5=E5=B9=B2=E9=AB=98?= =?UTF-8?q?=E9=80=9F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/mapk/core/SimpleKMapperTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/kotlin/mapk/core/SimpleKMapperTest.kt b/src/test/kotlin/mapk/core/SimpleKMapperTest.kt index d63caa3..d90be59 100644 --- a/src/test/kotlin/mapk/core/SimpleKMapperTest.kt +++ b/src/test/kotlin/mapk/core/SimpleKMapperTest.kt @@ -6,7 +6,6 @@ import com.wrongwrong.mapk.annotations.KConstructor import com.wrongwrong.mapk.core.KMapper import java.math.BigInteger import kotlin.reflect.full.isSubclassOf -import kotlin.reflect.full.primaryConstructor import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested @@ -71,7 +70,7 @@ class SimpleKMapperTest { private val mappers: Set> = setOf( KMapper(SimpleDst::class), - KMapper(SimpleDst::class.primaryConstructor!!), + KMapper(::SimpleDst), KMapper((SimpleDst)::factory), KMapper(this::instanceFunction), KMapper(SimpleDstExt::class) From 4c626b18c332d997753ca1f9e03e30eb01fb85d8 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 12:22:09 +0900 Subject: [PATCH 14/27] =?UTF-8?q?=E8=B2=AC=E5=8B=99=E3=82=92=E6=95=B4?= =?UTF-8?q?=E7=90=86=E3=81=97=E3=80=81companion=20object=E3=81=8C=E5=85=A5?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E5=A0=B4=E5=90=88=E3=81=AB=E5=AF=BE=E5=BF=9C?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wrongwrong/mapk/core/KFunctionForCall.kt | 20 ++++---- .../com/wrongwrong/mapk/core/KMapper.kt | 48 ++++++++++--------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt index ec8a8a9..97fdab6 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt @@ -1,23 +1,25 @@ package com.wrongwrong.mapk.core import kotlin.reflect.KFunction +import kotlin.reflect.KParameter import kotlin.reflect.jvm.isAccessible -class KFunctionForCall( - private val function: KFunction, - parameterSize: Int, - instance: Any? = null -) { +class KFunctionForCall(private val function: KFunction, instance: Any? = null) { + val parameters: List = function.parameters private val originalArray: Array val argumentArray: Array get() = originalArray.copyOf() init { // この関数には確実にアクセスするためアクセシビリティ書き換え function.isAccessible = true - - // 必要が有ればinstanceを先に、無ければ - originalArray = Array(parameterSize) { if (it == 0 && instance != null) instance else null } + originalArray = if (instance != null) { + Array(parameters.size) { if (it == 0) instance else null } + } else { + Array(parameters.size) { null } + } } - fun call(arguments: Array): T = function.call(*arguments) + fun call(arguments: Array): T { + return function.call(*arguments) + } } diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 9b67c17..3cf99c7 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -16,28 +16,31 @@ import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor import kotlin.reflect.jvm.isAccessible -class KMapper(function: KFunction, propertyNameConverter: (String) -> String = { it }) { +class KMapper private constructor( + private val function: KFunctionForCall, + propertyNameConverter: (String) -> String = { it } +) { + constructor(function: KFunction, propertyNameConverter: (String) -> String = { it }) : this( + KFunctionForCall(function), propertyNameConverter + ) + constructor(clazz: KClass, propertyNameConverter: (String) -> String = { it }) : this( getTarget(clazz), propertyNameConverter ) - private val parameterMap: Map> - private val functionForCall: KFunctionForCall - - init { - val params = function.parameters - functionForCall = KFunctionForCall(function, params.size) // TODO: 必要に応じてインスタンスを渡す - - parameterMap = params.filter { it.kind != KParameter.Kind.INSTANCE }.associate { + private val parameterMap: Map> = function.parameters + .filter { it.kind != KParameter.Kind.INSTANCE } + .associate { (it.findAnnotation()?.value ?: propertyNameConverter(it.name!!)) to ParameterForMap.newInstance(it) } + init { if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") } fun map(srcMap: Map): T { - val array: Array = functionForCall.argumentArray + val array: Array = function.argumentArray srcMap.forEach { (key, value) -> parameterMap[key]?.let { param -> @@ -46,17 +49,17 @@ class KMapper(function: KFunction, propertyNameConverter: (String) - } } - return functionForCall.call(array) + return function.call(array) } fun map(srcPair: Pair): T = parameterMap.getValue(srcPair.first).let { - val array: Array = functionForCall.argumentArray + val array: Array = function.argumentArray array[it.param.index] = srcPair.second?.let { value -> mapObject(it, value) } - functionForCall.call(array) + function.call(array) } fun map(src: Any): T { - val array: Array = functionForCall.argumentArray + val array: Array = function.argumentArray src::class.memberProperties.forEach { property -> if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { @@ -67,11 +70,11 @@ class KMapper(function: KFunction, propertyNameConverter: (String) - } } - return functionForCall.call(array) + return function.call(array) } fun map(vararg args: Any): T { - val array: Array = functionForCall.argumentArray + val array: Array = function.argumentArray listOf(*args).forEach { arg -> when (arg) { @@ -95,7 +98,7 @@ class KMapper(function: KFunction, propertyNameConverter: (String) - } } - return functionForCall.call(array) + return function.call(array) } } @@ -112,20 +115,21 @@ private fun KProperty1<*, *>.getAccessibleGetter(): KProperty1.Getter<*, *> { } @Suppress("UNCHECKED_CAST") -internal fun getTarget(clazz: KClass): KFunction { - val factoryConstructor: List> = +internal fun getTarget(clazz: KClass): KFunctionForCall { + val factoryConstructor: List> = clazz.companionObjectInstance?.let { companionObject -> companionObject::class.functions .filter { it.annotations.any { annotation -> annotation is KConstructor } } - .map { KFunctionWithInstance(it, companionObject) as KFunction } + .map { KFunctionForCall(it, companionObject) as KFunctionForCall } } ?: emptyList() - val constructors: List> = factoryConstructor + clazz.constructors + val constructors: List> = factoryConstructor + clazz.constructors .filter { it.annotations.any { annotation -> annotation is KConstructor } } + .map { KFunctionForCall(it) } if (constructors.size == 1) return constructors.single() - if (constructors.isEmpty()) return clazz.primaryConstructor!! + if (constructors.isEmpty()) return KFunctionForCall(clazz.primaryConstructor!!) throw IllegalArgumentException("Find multiple target.") } From 6805eebfe3e2d6ae83c6ed3504c52b9ce8374907 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 12:40:24 +0900 Subject: [PATCH 15/27] =?UTF-8?q?=E6=96=B9=E5=BC=8F=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=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/test/kotlin/mapk/core/GetTargetTest.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/test/kotlin/mapk/core/GetTargetTest.kt b/src/test/kotlin/mapk/core/GetTargetTest.kt index 7214d5e..54057a0 100644 --- a/src/test/kotlin/mapk/core/GetTargetTest.kt +++ b/src/test/kotlin/mapk/core/GetTargetTest.kt @@ -3,8 +3,12 @@ package mapk.core import com.wrongwrong.mapk.annotations.KConstructor +import com.wrongwrong.mapk.core.KFunctionForCall import com.wrongwrong.mapk.core.getTarget +import kotlin.reflect.KFunction +import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor +import kotlin.reflect.jvm.isAccessible import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.DisplayName @@ -27,26 +31,34 @@ class MultipleConstructorDst @KConstructor constructor(val argument: Int) { @KConstructor constructor(argument: String) : this(argument.toInt()) } +@Suppress("UNCHECKED_CAST") @DisplayName("クラスからのコンストラクタ抽出関連テスト") class GetTargetTest { + private fun KFunctionForCall.getTargetFunction(): KFunction { + return this::class.memberProperties.first { it.name == "function" }.getter.let { + it.isAccessible = true + it.call(this) as KFunction + } + } + @Test @DisplayName("セカンダリコンストラクタからの取得テスト") fun testGetFromSecondaryConstructor() { - val function = getTarget(SecondaryConstructorDst::class) + val function = getTarget(SecondaryConstructorDst::class).getTargetFunction() assertTrue(function.annotations.any { it is KConstructor }) } @Test @DisplayName("ファクトリーメソッドからの取得テスト") fun testGetFromFactoryMethod() { - val function = getTarget(CompanionFactoryDst::class) + val function = getTarget(SecondaryConstructorDst::class).getTargetFunction() assertTrue(function.annotations.any { it is KConstructor }) } @Test @DisplayName("無指定でプライマリコンストラクタからの取得テスト") fun testGetFromPrimaryConstructor() { - val function = getTarget(ConstructorDst::class) + val function = getTarget(ConstructorDst::class).getTargetFunction() assertEquals(ConstructorDst::class.primaryConstructor, function) } From 4d27b4dc05c0acbfb627c934e33a75025fb8f7a2 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 13:31:00 +0900 Subject: [PATCH 16/27] =?UTF-8?q?=E3=82=A8=E3=82=A4=E3=83=AA=E3=82=A2?= =?UTF-8?q?=E3=82=B9=E6=9C=89=E3=82=8A=E3=81=A7=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/mapk/core/ParamAliasTest.kt | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/test/kotlin/mapk/core/ParamAliasTest.kt diff --git a/src/test/kotlin/mapk/core/ParamAliasTest.kt b/src/test/kotlin/mapk/core/ParamAliasTest.kt new file mode 100644 index 0000000..5ddf8fc --- /dev/null +++ b/src/test/kotlin/mapk/core/ParamAliasTest.kt @@ -0,0 +1,45 @@ +package mapk.core + +import com.wrongwrong.mapk.annotations.KPropertyAlias +import com.wrongwrong.mapk.core.KMapper +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test + +private data class AliasedDst( + val arg1: Double, + @param:KPropertyAlias("arg3") val arg2: Int +) + +private data class AliasedSrc( + @get:KPropertyAlias("arg1") val arg2: Double, + val arg3: Int +) + +@DisplayName("エイリアスを貼った場合のテスト") +class ParamAliasTest { + @Test + @DisplayName("パラメータにエイリアスを貼った場合") + fun paramAliasTest() { + val src = mapOf( + "arg1" to 1.0, + "arg2" to "2", + "arg3" to 3 + ) + + val result = KMapper(::AliasedDst).map(src) + + assertEquals(1.0, result.arg1) + assertEquals(3, result.arg2) + } + + @Test + @DisplayName("ゲッターにエイリアスを貼った場合") + fun getAliasTest() { + val src = AliasedSrc(1.0, 2) + val result = KMapper(::AliasedDst).map(src) + + assertEquals(1.0, result.arg1) + assertEquals(2, result.arg2) + } +} From 41487bac27b4d72d9ab9dd6c5607b587d94fcdd0 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 14:27:41 +0900 Subject: [PATCH 17/27] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E5=86=85=E3=81=A7=E3=81=AFindex=E3=81=97=E3=81=8B=E5=88=A9?= =?UTF-8?q?=E7=94=A8=E3=81=97=E3=81=AA=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E3=81=9F=E3=82=81=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/wrongwrong/mapk/core/KMapper.kt | 12 ++++++------ .../com/wrongwrong/mapk/core/ParameterForMap.kt | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 3cf99c7..e95f5cd 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -45,7 +45,7 @@ class KMapper private constructor( srcMap.forEach { (key, value) -> parameterMap[key]?.let { param -> // 取得した内容がnullでなければ適切にmapする - array[param.param.index] = value?.let { mapObject(param, it) } + array[param.index] = value?.let { mapObject(param, it) } } } @@ -54,7 +54,7 @@ class KMapper private constructor( fun map(srcPair: Pair): T = parameterMap.getValue(srcPair.first).let { val array: Array = function.argumentArray - array[it.param.index] = srcPair.second?.let { value -> mapObject(it, value) } + array[it.index] = srcPair.second?.let { value -> mapObject(it, value) } function.call(array) } @@ -65,7 +65,7 @@ class KMapper private constructor( if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { val getter = property.getAccessibleGetter() parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { - array[it.param.index] = getter.call(src)?.let { value -> mapObject(it, value) } + array[it.index] = getter.call(src)?.let { value -> mapObject(it, value) } } } } @@ -81,17 +81,17 @@ class KMapper private constructor( is Map<*, *> -> arg.forEach { (key, value) -> parameterMap[key]?.let { param -> // 取得した内容がnullでなければ適切にmapする - array[param.param.index] = value?.let { mapObject(param, it) } + array[param.index] = value?.let { mapObject(param, it) } } } is Pair<*, *> -> parameterMap.getValue(arg.first as String).let { - array[it.param.index] = arg.second?.let { value -> mapObject(it, value) } + array[it.index] = arg.second?.let { value -> mapObject(it, value) } } else -> arg::class.memberProperties.forEach { property -> if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { val getter = property.getAccessibleGetter() parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { - array[it.param.index] = getter.call(arg)?.let { value -> mapObject(it, value) } + array[it.index] = getter.call(arg)?.let { value -> mapObject(it, value) } } } } diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt index 5345716..4f3d0b3 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/ParameterForMap.kt @@ -10,7 +10,7 @@ import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.staticFunctions import kotlin.reflect.jvm.isAccessible -internal class ParameterForMap private constructor(val param: KParameter, val clazz: KClass) { +internal class ParameterForMap private constructor(val index: Int, val clazz: KClass) { val javaClazz: Class by lazy { clazz.java } @@ -25,7 +25,7 @@ internal class ParameterForMap private constructor(val param: KParamete companion object { fun newInstance(param: KParameter): ParameterForMap<*> { - return ParameterForMap(param, param.type.classifier as KClass<*>) + return ParameterForMap(param.index, param.type.classifier as KClass<*>) } } } From 9569f7f66a3988cf0694165c3d011e00a20bb496 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 14:57:24 +0900 Subject: [PATCH 18/27] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=83=91=E3=83=86?= =?UTF-8?q?=E3=82=A3=E3=81=A8=E3=82=B2=E3=83=83=E3=82=BF=E3=83=BC=E3=81=AE?= =?UTF-8?q?alias=E3=81=AF=E3=81=9D=E3=82=8C=E3=81=9E=E3=82=8C=E5=88=A5?= =?UTF-8?q?=E3=82=A2=E3=83=8E=E3=83=86=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=81=A7=E6=89=B1=E3=81=86=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/wrongwrong/mapk/annotations/KGetterAlias.kt | 5 +++++ .../kotlin/com/wrongwrong/mapk/annotations/KPropertyAlias.kt | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/wrongwrong/mapk/annotations/KGetterAlias.kt diff --git a/src/main/kotlin/com/wrongwrong/mapk/annotations/KGetterAlias.kt b/src/main/kotlin/com/wrongwrong/mapk/annotations/KGetterAlias.kt new file mode 100644 index 0000000..6bf6815 --- /dev/null +++ b/src/main/kotlin/com/wrongwrong/mapk/annotations/KGetterAlias.kt @@ -0,0 +1,5 @@ +package com.wrongwrong.mapk.annotations + +@Target(AnnotationTarget.PROPERTY) +@Retention(AnnotationRetention.RUNTIME) +annotation class KGetterAlias(val value: String) diff --git a/src/main/kotlin/com/wrongwrong/mapk/annotations/KPropertyAlias.kt b/src/main/kotlin/com/wrongwrong/mapk/annotations/KPropertyAlias.kt index 5ada366..eab10fa 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/annotations/KPropertyAlias.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/annotations/KPropertyAlias.kt @@ -1,5 +1,5 @@ package com.wrongwrong.mapk.annotations -@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.PROPERTY_GETTER) +@Target(AnnotationTarget.VALUE_PARAMETER) @Retention(AnnotationRetention.RUNTIME) annotation class KPropertyAlias(val value: String) From 2eca20f77920661e1cb5c819b891ad9847cb7cd5 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 14:57:57 +0900 Subject: [PATCH 19/27] =?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/mapk/core/ParamAliasTest.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/mapk/core/ParamAliasTest.kt b/src/test/kotlin/mapk/core/ParamAliasTest.kt index 5ddf8fc..3929b90 100644 --- a/src/test/kotlin/mapk/core/ParamAliasTest.kt +++ b/src/test/kotlin/mapk/core/ParamAliasTest.kt @@ -1,5 +1,6 @@ package mapk.core +import com.wrongwrong.mapk.annotations.KGetterAlias import com.wrongwrong.mapk.annotations.KPropertyAlias import com.wrongwrong.mapk.core.KMapper import org.junit.jupiter.api.Assertions.assertEquals @@ -12,7 +13,8 @@ private data class AliasedDst( ) private data class AliasedSrc( - @get:KPropertyAlias("arg1") val arg2: Double, + @KGetterAlias("arg1") + val arg2: Double, val arg3: Int ) From 364e4d04499823a331035eab63252ee34eb99457 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 15:37:47 +0900 Subject: [PATCH 20/27] =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=8B?= =?UTF-8?q?=E3=82=89=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=99=E9=9A=9B=E3=81=AF?= =?UTF-8?q?javaGetter=E3=82=92=E4=BD=BF=E3=81=86=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wrongwrong/mapk/core/KMapper.kt | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index e95f5cd..1330e7d 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -1,8 +1,10 @@ package com.wrongwrong.mapk.core import com.wrongwrong.mapk.annotations.KConstructor +import com.wrongwrong.mapk.annotations.KGetterAlias import com.wrongwrong.mapk.annotations.KPropertyAlias import com.wrongwrong.mapk.annotations.KPropertyIgnore +import java.lang.reflect.Method import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter @@ -15,6 +17,7 @@ import kotlin.reflect.full.isSuperclassOf import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor import kotlin.reflect.jvm.isAccessible +import kotlin.reflect.jvm.javaGetter class KMapper private constructor( private val function: KFunctionForCall, @@ -62,10 +65,12 @@ class KMapper private constructor( val array: Array = function.argumentArray src::class.memberProperties.forEach { property -> - if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { - val getter = property.getAccessibleGetter() - parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { - array[it.index] = getter.call(src)?.let { value -> mapObject(it, value) } + val javaGetter: Method? = property.javaGetter + if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { + parameterMap[property.findAnnotation()?.value ?: property.name]?.let { + // javaGetterを呼び出す方が高速 + javaGetter.isAccessible = true + array[it.index] = javaGetter.invoke(src)?.let { value -> mapObject(it, value) } } } } @@ -88,10 +93,12 @@ class KMapper private constructor( array[it.index] = arg.second?.let { value -> mapObject(it, value) } } else -> arg::class.memberProperties.forEach { property -> - if (property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { - val getter = property.getAccessibleGetter() - parameterMap[getter.findAnnotation()?.value ?: property.name]?.let { - array[it.index] = getter.call(arg)?.let { value -> mapObject(it, value) } + val javaGetter: Method? = property.javaGetter + if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { + parameterMap[property.findAnnotation()?.value ?: property.name]?.let { + // javaGetterを呼び出す方が高速 + javaGetter.isAccessible = true + array[it.index] = javaGetter.invoke(arg)?.let { value -> mapObject(it, value) } } } } From 0c812607e6f0d0c2ff0f4968b0d979fa241a26c5 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 16:14:35 +0900 Subject: [PATCH 21/27] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AB=E3=81=AA?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E3=81=9F=E3=82=81=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 1330e7d..050f7f2 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -115,12 +115,6 @@ private fun Collection>.filterTargets(): Collection.getAccessibleGetter(): KProperty1.Getter<*, *> { - // アクセス制限の有るクラスではpublicなプロパティでもゲッターにアクセスできない場合が有るため、アクセス可能にして使う - getter.isAccessible = true - return getter -} - @Suppress("UNCHECKED_CAST") internal fun getTarget(clazz: KClass): KFunctionForCall { val factoryConstructor: List> = From 6d79fb333767c6c01b1f1e08df0631786f46bd93 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 16:18:16 +0900 Subject: [PATCH 22/27] =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=85=B1?= =?UTF-8?q?=E9=80=9A=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wrongwrong/mapk/core/KMapper.kt | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 050f7f2..21999ca 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -42,6 +42,19 @@ class KMapper private constructor( if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") } + private fun KClass<*>.bindParameters(targetArray: Array, instance: Any) { + memberProperties.forEach { property -> + val javaGetter: Method? = property.javaGetter + if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { + parameterMap[property.findAnnotation()?.value ?: property.name]?.let { + // javaGetterを呼び出す方が高速 + javaGetter.isAccessible = true + targetArray[it.index] = javaGetter.invoke(instance)?.let { value -> mapObject(it, value) } + } + } + } + } + fun map(srcMap: Map): T { val array: Array = function.argumentArray @@ -63,18 +76,7 @@ class KMapper private constructor( fun map(src: Any): T { val array: Array = function.argumentArray - - src::class.memberProperties.forEach { property -> - val javaGetter: Method? = property.javaGetter - if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { - parameterMap[property.findAnnotation()?.value ?: property.name]?.let { - // javaGetterを呼び出す方が高速 - javaGetter.isAccessible = true - array[it.index] = javaGetter.invoke(src)?.let { value -> mapObject(it, value) } - } - } - } - + src::class.bindParameters(array, src) return function.call(array) } @@ -92,16 +94,7 @@ class KMapper private constructor( is Pair<*, *> -> parameterMap.getValue(arg.first as String).let { array[it.index] = arg.second?.let { value -> mapObject(it, value) } } - else -> arg::class.memberProperties.forEach { property -> - val javaGetter: Method? = property.javaGetter - if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { - parameterMap[property.findAnnotation()?.value ?: property.name]?.let { - // javaGetterを呼び出す方が高速 - javaGetter.isAccessible = true - array[it.index] = javaGetter.invoke(arg)?.let { value -> mapObject(it, value) } - } - } - } + else -> arg::class.bindParameters(array, arg) } } From 930877d168c30f2ce69e51deb337bd21a6dc8222 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 16:19:37 +0900 Subject: [PATCH 23/27] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AB=E3=81=AA?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E5=87=A6=E7=90=86=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 21999ca..34e7db8 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -102,12 +102,6 @@ class KMapper private constructor( } } -private fun Collection>.filterTargets(): Collection> { - return filter { - it.visibility == KVisibility.PUBLIC && it.annotations.none { annotation -> annotation is KPropertyIgnore } - } -} - @Suppress("UNCHECKED_CAST") internal fun getTarget(clazz: KClass): KFunctionForCall { val factoryConstructor: List> = From 2a92908005b34660430c91e6403ac0557da40276 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 16:23:56 +0900 Subject: [PATCH 24/27] =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=85=B1?= =?UTF-8?q?=E9=80=9A=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wrongwrong/mapk/core/KMapper.kt | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index 34e7db8..ef679f8 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -8,7 +8,6 @@ import java.lang.reflect.Method import kotlin.reflect.KClass import kotlin.reflect.KFunction import kotlin.reflect.KParameter -import kotlin.reflect.KProperty1 import kotlin.reflect.KVisibility import kotlin.reflect.full.companionObjectInstance import kotlin.reflect.full.findAnnotation @@ -55,16 +54,18 @@ class KMapper private constructor( } } - fun map(srcMap: Map): T { - val array: Array = function.argumentArray - - srcMap.forEach { (key, value) -> + private fun Map<*, *>.bindParameters(targetArray: Array) { + forEach { (key, value) -> parameterMap[key]?.let { param -> // 取得した内容がnullでなければ適切にmapする - array[param.index] = value?.let { mapObject(param, it) } + targetArray[param.index] = value?.let { mapObject(param, it) } } } + } + fun map(srcMap: Map): T { + val array: Array = function.argumentArray + srcMap.bindParameters(array) return function.call(array) } @@ -85,12 +86,7 @@ class KMapper private constructor( listOf(*args).forEach { arg -> when (arg) { - is Map<*, *> -> arg.forEach { (key, value) -> - parameterMap[key]?.let { param -> - // 取得した内容がnullでなければ適切にmapする - array[param.index] = value?.let { mapObject(param, it) } - } - } + is Map<*, *> -> arg.bindParameters(array) is Pair<*, *> -> parameterMap.getValue(arg.first as String).let { array[it.index] = arg.second?.let { value -> mapObject(it, value) } } From 79500bcbacdd6cbb5192ce227e46aa72b6da17c7 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 16:31:07 +0900 Subject: [PATCH 25/27] =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=85=B1?= =?UTF-8?q?=E9=80=9A=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/wrongwrong/mapk/core/KMapper.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index ef679f8..c006181 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -63,16 +63,22 @@ class KMapper private constructor( } } + private fun bindParameters(targetArray: Array, srcPair: Pair<*, *>) { + parameterMap.getValue(srcPair.first.toString()).let { + targetArray[it.index] = srcPair.second?.let { value -> mapObject(it, value) } + } + } + fun map(srcMap: Map): T { val array: Array = function.argumentArray srcMap.bindParameters(array) return function.call(array) } - fun map(srcPair: Pair): T = parameterMap.getValue(srcPair.first).let { + fun map(srcPair: Pair): T { val array: Array = function.argumentArray - array[it.index] = srcPair.second?.let { value -> mapObject(it, value) } - function.call(array) + bindParameters(array, srcPair) + return function.call(array) } fun map(src: Any): T { @@ -87,9 +93,7 @@ class KMapper private constructor( listOf(*args).forEach { arg -> when (arg) { is Map<*, *> -> arg.bindParameters(array) - is Pair<*, *> -> parameterMap.getValue(arg.first as String).let { - array[it.index] = arg.second?.let { value -> mapObject(it, value) } - } + is Pair<*, *> -> bindParameters(array, arg) else -> arg::class.bindParameters(array, arg) } } From fcac5f52c0b7b413af6ae2c20b91a6f48f1f0efc Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 16:33:48 +0900 Subject: [PATCH 26/27] =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=85=B1?= =?UTF-8?q?=E9=80=9A=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wrongwrong/mapk/core/KMapper.kt | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt index c006181..9a1934c 100644 --- a/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt +++ b/src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt @@ -15,7 +15,6 @@ import kotlin.reflect.full.functions import kotlin.reflect.full.isSuperclassOf import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor -import kotlin.reflect.jvm.isAccessible import kotlin.reflect.jvm.javaGetter class KMapper private constructor( @@ -41,21 +40,21 @@ class KMapper private constructor( if (parameterMap.isEmpty()) throw IllegalArgumentException("This function is not require arguments.") } - private fun KClass<*>.bindParameters(targetArray: Array, instance: Any) { - memberProperties.forEach { property -> + private fun bindParameters(targetArray: Array, src: Any) { + src::class.memberProperties.forEach { property -> val javaGetter: Method? = property.javaGetter if (javaGetter != null && property.visibility == KVisibility.PUBLIC && property.annotations.none { annotation -> annotation is KPropertyIgnore }) { parameterMap[property.findAnnotation()?.value ?: property.name]?.let { // javaGetterを呼び出す方が高速 javaGetter.isAccessible = true - targetArray[it.index] = javaGetter.invoke(instance)?.let { value -> mapObject(it, value) } + targetArray[it.index] = javaGetter.invoke(src)?.let { value -> mapObject(it, value) } } } } } - private fun Map<*, *>.bindParameters(targetArray: Array) { - forEach { (key, value) -> + private fun bindParameters(targetArray: Array, src: Map<*, *>) { + src.forEach { (key, value) -> parameterMap[key]?.let { param -> // 取得した内容がnullでなければ適切にmapする targetArray[param.index] = value?.let { mapObject(param, it) } @@ -71,7 +70,7 @@ class KMapper private constructor( fun map(srcMap: Map): T { val array: Array = function.argumentArray - srcMap.bindParameters(array) + bindParameters(array, srcMap) return function.call(array) } @@ -83,7 +82,7 @@ class KMapper private constructor( fun map(src: Any): T { val array: Array = function.argumentArray - src::class.bindParameters(array, src) + bindParameters(array, src) return function.call(array) } @@ -92,9 +91,9 @@ class KMapper private constructor( listOf(*args).forEach { arg -> when (arg) { - is Map<*, *> -> arg.bindParameters(array) + is Map<*, *> -> bindParameters(array, arg) is Pair<*, *> -> bindParameters(array, arg) - else -> arg::class.bindParameters(array, arg) + else -> bindParameters(array, arg) } } From 034da25f4769fba019fb4d618e8518286a4d64da Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 23 Feb 2020 17:56:31 +0900 Subject: [PATCH 27/27] =?UTF-8?q?readme=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0eb326f..081984c 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ class Dst( ### Set alias on map #### for getter ```kotlin -class Src(@get:PropertyAlias("aliased") val str: String) +class Src(@KGetterAlias("aliased") val str: String) class Dst(val aliased: String) ``` @@ -53,7 +53,7 @@ class Dst(val aliased: String) ```kotlin class Src(val str: String) -class Dst(@param:PropertyAlias("str") private val _src: String) { +class Dst(@param:KPropertyAlias("str") private val _src: String) { val src = _src.someArrangement } ``` @@ -64,7 +64,7 @@ val srcMap = mapOf("snake_case" to "SnakeCase") class Dst(val snakeCase: String) -val dst: Dst = Mapper(DataClass::class.primaryConstructor!!) { it.toSnakeCase }.map(src) +val dst: Dst = Mapper(::DataClass) { it.toSnakeCase }.map(src) ``` ### Map param to another class @@ -72,7 +72,7 @@ val dst: Dst = Mapper(DataClass::class.primaryConstructor!!) { it.toSnakeCase }. ```kotlin class CreatorClass @SingleArgCreator constructor(val arg: String) { companion object { - @SingleArgCreator + @KConverter fun fromInt(arg: Int): CreatorClass { return CreatorClass(arg.toString) }