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

Commit 7264eab

Browse files
authored
Merge pull request #25 from k163377/functions
Commonization and optimization of functions.
2 parents b7937e9 + 08a18ef commit 7264eab

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.mapk"
9-
version = "0.12"
9+
version = "0.13"
1010

1111
java {
1212
sourceCompatibility = JavaVersion.VERSION_1_8
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mapk.core
2+
3+
import kotlin.reflect.KClass
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.KParameter
6+
import kotlin.reflect.full.companionObject
7+
import kotlin.reflect.full.functions
8+
9+
inline fun <reified A : Annotation> KClass<*>.getAnnotatedFunctionsFromCompanionObject(): Pair<Any, List<KFunction<*>>>? {
10+
return this.companionObject?.let { companionObject ->
11+
val temp = companionObject.functions.filter { functions -> functions.annotations.any { it is A } }
12+
13+
if (temp.isEmpty()) {
14+
// 空ならその後の処理をしてもしょうがないのでnullに合わせる
15+
null
16+
} else {
17+
companionObject.objectInstance!! to temp
18+
}
19+
}
20+
}
21+
22+
inline fun <reified A : Annotation, T> Collection<KFunction<T>>.getAnnotatedFunctions(): List<KFunction<T>> {
23+
return filter { function -> function.annotations.any { it is A } }
24+
}
25+
26+
fun KParameter.getKClass(): KClass<*> = type.classifier as KClass<*>

src/main/kotlin/com/mapk/core/KFunctionForCall.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import com.mapk.core.internal.isUseDefaultArgument
1010
import kotlin.reflect.KClass
1111
import kotlin.reflect.KFunction
1212
import kotlin.reflect.KParameter
13-
import kotlin.reflect.full.companionObjectInstance
1413
import kotlin.reflect.full.findAnnotation
15-
import kotlin.reflect.full.functions
1614
import kotlin.reflect.full.primaryConstructor
1715
import kotlin.reflect.jvm.isAccessible
1816
import kotlin.reflect.jvm.jvmName
@@ -91,17 +89,15 @@ class KFunctionForCall<T> internal constructor(
9189
internal fun <T : Any> KClass<T>.toKConstructor(parameterNameConverter: ParameterNameConverter): KFunctionForCall<T> {
9290
val constructors = ArrayList<KFunctionForCall<T>>()
9391

94-
this.companionObjectInstance?.let { companionObject ->
95-
companionObject::class.functions
96-
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
97-
.forEach {
98-
constructors.add(KFunctionForCall(it, parameterNameConverter, companionObject) as KFunctionForCall<T>)
99-
}
92+
this.getAnnotatedFunctionsFromCompanionObject<KConstructor>()?.let { (instance, functions) ->
93+
functions.forEach {
94+
constructors.add(KFunctionForCall(it as KFunction<T>, parameterNameConverter, instance))
95+
}
10096
}
10197

102-
this.constructors
103-
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
104-
.forEach { constructors.add(KFunctionForCall(it, parameterNameConverter)) }
98+
this.constructors.getAnnotatedFunctions<KConstructor, T>().forEach {
99+
constructors.add(KFunctionForCall(it, parameterNameConverter))
100+
}
105101

106102
if (constructors.size == 1) return constructors.single()
107103

@@ -127,12 +123,12 @@ private fun KParameter.toArgumentBinder(parameterNameConverter: ParameterNameCon
127123
parameterNameConverter.toSimple()
128124
}
129125

130-
ArgumentBinder.Function((type.classifier as KClass<*>).toKConstructor(converter), index, annotations)
126+
ArgumentBinder.Function(getKClass().toKConstructor(converter), index, annotations)
131127
} ?: ArgumentBinder.Value(
132128
index,
133129
annotations,
134130
isOptional,
135131
parameterNameConverter.convert(name),
136-
type.classifier as KClass<*>
132+
getKClass()
137133
)
138134
}

0 commit comments

Comments
 (0)