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

Commit 3521398

Browse files
committed
インスタンスの型バリデーションを追加
1 parent 6e81a1f commit 3521398

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/main/kotlin/com/mapk/fastkfunction/SingleArgFastKFunction.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.lang.reflect.Method
55
import java.lang.reflect.Modifier
66
import kotlin.reflect.KFunction
77
import kotlin.reflect.KParameter
8+
import kotlin.reflect.full.isSuperclassOf
89
import kotlin.reflect.jvm.isAccessible
910
import kotlin.reflect.jvm.javaConstructor
1011
import kotlin.reflect.jvm.javaMethod
@@ -107,9 +108,33 @@ sealed class SingleArgFastKFunction<T> {
107108
return when {
108109
parameters[0].kind == KParameter.Kind.INSTANCE ->
109110
instance
110-
?.let { InstanceFunction(parameters[1], method, it) }
111+
?.let {
112+
val instanceClazz = it::class
113+
114+
method.declaringClass.kotlin.also { requiredClazz ->
115+
if (!requiredClazz.isSuperclassOf(instanceClazz))
116+
throw IllegalArgumentException(
117+
"INSTANCE parameter required ${instanceClazz.simpleName}, " +
118+
"but ${instanceClazz.simpleName} is present."
119+
)
120+
}
121+
122+
InstanceFunction(parameters[1], method, it)
123+
}
111124
?: throw IllegalArgumentException("Function requires INSTANCE parameter, but is not present.")
112-
instance != null -> InstanceFunction(parameters[0], method, instance)
125+
instance != null -> {
126+
val instanceClazz = instance::class
127+
128+
method.declaringClass.kotlin.also {
129+
if (!it.isSuperclassOf(instanceClazz))
130+
throw IllegalArgumentException(
131+
"INSTANCE parameter required ${it.simpleName}, " +
132+
"but ${instanceClazz.simpleName} is present."
133+
)
134+
}
135+
136+
InstanceFunction(parameters[0], method, instance)
137+
}
113138
else -> Function(parameters[0], function)
114139
}
115140
}

0 commit comments

Comments
 (0)