Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.mapk"
version = "0.10"
version = "0.15"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -30,7 +30,7 @@ repositories {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation(kotlin("reflect"))
implementation("com.github.ProjectMapK:Shared:0.5")
implementation("com.github.ProjectMapK:Shared:0.6")

// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.0") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.mapk.annotations
@Target(AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class KConstructor
annotation class KConverter
38 changes: 4 additions & 34 deletions src/main/kotlin/com/mapk/kmapper/KMapper.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.mapk.kmapper

import com.mapk.annotations.KConstructor
import com.mapk.annotations.KGetterAlias
import com.mapk.annotations.KGetterIgnore
import com.mapk.annotations.KParameterAlias
import com.mapk.core.ArgumentBucket
import com.mapk.core.EnumMapper
import com.mapk.core.KFunctionForCall
import com.mapk.core.getAliasOrName
import com.mapk.core.toKConstructor
import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
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
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaGetter

class KMapper<T : Any> private constructor(
Expand All @@ -29,15 +25,12 @@ class KMapper<T : Any> private constructor(
)

constructor(clazz: KClass<T>, propertyNameConverter: (String) -> String = { it }) : this(
getTarget(clazz), propertyNameConverter
clazz.toKConstructor(), propertyNameConverter
)

private val parameterMap: Map<String, ParameterForMap<*>> = function.parameters
.filter { it.kind != KParameter.Kind.INSTANCE }
.associate {
(it.findAnnotation<KParameterAlias>()?.value ?: propertyNameConverter(it.name!!)) to
ParameterForMap.newInstance(it)
}
.associate { (propertyNameConverter(it.getAliasOrName()!!)) to ParameterForMap.newInstance(it) }

private fun bindArguments(argumentBucket: ArgumentBucket, src: Any) {
src::class.memberProperties.forEach outer@{ property ->
Expand Down Expand Up @@ -117,29 +110,6 @@ class KMapper<T : Any> private constructor(
}
}

@Suppress("UNCHECKED_CAST")
internal fun <T : Any> getTarget(clazz: KClass<T>): KFunctionForCall<T> {
val factoryConstructor: List<KFunctionForCall<T>> =
clazz.companionObjectInstance?.let { companionObject ->
companionObject::class.functions
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
.map { KFunctionForCall(
it,
companionObject
) as KFunctionForCall<T> }
} ?: emptyList()

val constructors: List<KFunctionForCall<T>> = 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 KFunctionForCall(clazz.primaryConstructor!!)

throw IllegalArgumentException("Find multiple target.")
}

private fun <T : Any, R : Any> mapObject(param: ParameterForMap<R>, value: T): Any? {
val valueClazz: KClass<*> = value::class

Expand Down
69 changes: 0 additions & 69 deletions src/test/kotlin/com/mapk/kmapper/GetTargetTest.kt

This file was deleted.