@@ -5,6 +5,7 @@ import java.lang.reflect.Method
5
5
import java.lang.reflect.Modifier
6
6
import kotlin.reflect.KFunction
7
7
import kotlin.reflect.KParameter
8
+ import kotlin.reflect.full.isSuperclassOf
8
9
import kotlin.reflect.jvm.isAccessible
9
10
import kotlin.reflect.jvm.javaConstructor
10
11
import kotlin.reflect.jvm.javaMethod
@@ -107,9 +108,33 @@ sealed class SingleArgFastKFunction<T> {
107
108
return when {
108
109
parameters[0 ].kind == KParameter .Kind .INSTANCE ->
109
110
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
+ }
111
124
? : 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
+ }
113
138
else -> Function (parameters[0 ], function)
114
139
}
115
140
}
0 commit comments