Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 62b1f90

Browse files
authored
Merge pull request #12 from k163377/feature
Modifies.
2 parents 2800218 + 8f20c7c commit 62b1f90

File tree

4 files changed

+7
-106
lines changed

4 files changed

+7
-106
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.mapk"
9-
version = "0.10"
9+
version = "0.15"
1010

1111
java {
1212
sourceCompatibility = JavaVersion.VERSION_1_8
@@ -30,7 +30,7 @@ repositories {
3030
dependencies {
3131
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
3232
implementation(kotlin("reflect"))
33-
implementation("com.github.ProjectMapK:Shared:0.5")
33+
implementation("com.github.ProjectMapK:Shared:0.6")
3434

3535
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
3636
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.0") {

src/main/kotlin/com/mapk/annotations/KConstructor.kt renamed to src/main/kotlin/com/mapk/annotations/KConverter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package com.mapk.annotations
33
@Target(AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FUNCTION)
44
@Retention(AnnotationRetention.RUNTIME)
55
@MustBeDocumented
6-
annotation class KConstructor
6+
annotation class KConverter

src/main/kotlin/com/mapk/kmapper/KMapper.kt

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package com.mapk.kmapper
22

3-
import com.mapk.annotations.KConstructor
43
import com.mapk.annotations.KGetterAlias
54
import com.mapk.annotations.KGetterIgnore
6-
import com.mapk.annotations.KParameterAlias
75
import com.mapk.core.ArgumentBucket
86
import com.mapk.core.EnumMapper
97
import com.mapk.core.KFunctionForCall
8+
import com.mapk.core.getAliasOrName
9+
import com.mapk.core.toKConstructor
1010
import java.lang.reflect.Method
1111
import kotlin.reflect.KClass
1212
import kotlin.reflect.KFunction
1313
import kotlin.reflect.KParameter
1414
import kotlin.reflect.KVisibility
15-
import kotlin.reflect.full.companionObjectInstance
16-
import kotlin.reflect.full.findAnnotation
17-
import kotlin.reflect.full.functions
1815
import kotlin.reflect.full.isSuperclassOf
1916
import kotlin.reflect.full.memberProperties
20-
import kotlin.reflect.full.primaryConstructor
2117
import kotlin.reflect.jvm.javaGetter
2218

2319
class KMapper<T : Any> private constructor(
@@ -29,15 +25,12 @@ class KMapper<T : Any> private constructor(
2925
)
3026

3127
constructor(clazz: KClass<T>, propertyNameConverter: (String) -> String = { it }) : this(
32-
getTarget(clazz), propertyNameConverter
28+
clazz.toKConstructor(), propertyNameConverter
3329
)
3430

3531
private val parameterMap: Map<String, ParameterForMap<*>> = function.parameters
3632
.filter { it.kind != KParameter.Kind.INSTANCE }
37-
.associate {
38-
(it.findAnnotation<KParameterAlias>()?.value ?: propertyNameConverter(it.name!!)) to
39-
ParameterForMap.newInstance(it)
40-
}
33+
.associate { (propertyNameConverter(it.getAliasOrName()!!)) to ParameterForMap.newInstance(it) }
4134

4235
private fun bindArguments(argumentBucket: ArgumentBucket, src: Any) {
4336
src::class.memberProperties.forEach outer@{ property ->
@@ -117,29 +110,6 @@ class KMapper<T : Any> private constructor(
117110
}
118111
}
119112

120-
@Suppress("UNCHECKED_CAST")
121-
internal fun <T : Any> getTarget(clazz: KClass<T>): KFunctionForCall<T> {
122-
val factoryConstructor: List<KFunctionForCall<T>> =
123-
clazz.companionObjectInstance?.let { companionObject ->
124-
companionObject::class.functions
125-
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
126-
.map { KFunctionForCall(
127-
it,
128-
companionObject
129-
) as KFunctionForCall<T> }
130-
} ?: emptyList()
131-
132-
val constructors: List<KFunctionForCall<T>> = factoryConstructor + clazz.constructors
133-
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
134-
.map { KFunctionForCall(it) }
135-
136-
if (constructors.size == 1) return constructors.single()
137-
138-
if (constructors.isEmpty()) return KFunctionForCall(clazz.primaryConstructor!!)
139-
140-
throw IllegalArgumentException("Find multiple target.")
141-
}
142-
143113
private fun <T : Any, R : Any> mapObject(param: ParameterForMap<R>, value: T): Any? {
144114
val valueClazz: KClass<*> = value::class
145115

src/test/kotlin/com/mapk/kmapper/GetTargetTest.kt

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)