19
19
import kotlin .jvm .internal .Reflection ;
20
20
import kotlin .reflect .KCallable ;
21
21
import kotlin .reflect .KClass ;
22
+ import kotlin .reflect .KClassifier ;
22
23
import kotlin .reflect .KFunction ;
23
24
import kotlin .reflect .KParameter ;
24
25
import kotlin .reflect .KProperty ;
@@ -109,7 +110,7 @@ public boolean shouldApplyBoxing(KType type, boolean optional, KParameter compon
109
110
110
111
KType copyType = expandUnderlyingType (type );
111
112
112
- if (copyType .getClassifier ()instanceof KClass <?> kc && kc .isValue () || copyType .isMarkedNullable ()) {
113
+ if (copyType .getClassifier () instanceof KClass <?> kc && kc .isValue () || copyType .isMarkedNullable ()) {
113
114
return true ;
114
115
}
115
116
@@ -118,7 +119,7 @@ public boolean shouldApplyBoxing(KType type, boolean optional, KParameter compon
118
119
119
120
private static KType expandUnderlyingType (KType kotlinType ) {
120
121
121
- if (!(kotlinType .getClassifier ()instanceof KClass <?> kc ) || !kc .isValue ()) {
122
+ if (!(kotlinType .getClassifier () instanceof KClass <?> kc ) || !kc .isValue ()) {
122
123
return kotlinType ;
123
124
}
124
125
@@ -196,7 +197,43 @@ static class ValueBoxing {
196
197
*/
197
198
@ SuppressWarnings ("ConstantConditions" )
198
199
private ValueBoxing (BoxingRules rules , KParameter parameter ) {
199
- this (rules , parameter .getType (), (KClass <?>) parameter .getType ().getClassifier (), parameter .isOptional ());
200
+ this (rules , parameter .getType (), resolveClass (parameter .getType ()), parameter .isOptional ());
201
+ }
202
+
203
+ private static KClass <?> resolveClass (KType type ) {
204
+
205
+ if (type instanceof KClass <?> kc ) {
206
+ return kc ;
207
+ }
208
+
209
+ if (type instanceof KTypeParameter ktp ) {
210
+ return resolveClass (ktp .getUpperBounds ().get (0 ));
211
+ }
212
+
213
+ KClassifier classifier = type .getClassifier ();
214
+
215
+ if (classifier != null ) {
216
+ return resolveClass (classifier );
217
+ }
218
+
219
+ return JvmClassMappingKt .getKotlinClass (Object .class );
220
+ }
221
+
222
+ private static KClass <?> resolveClass (KClassifier classifier ) {
223
+
224
+ if (classifier instanceof KClass <?> kc ) {
225
+ return kc ;
226
+ }
227
+
228
+ if (classifier instanceof KTypeParameter ktp ) {
229
+ return resolveClass (ktp .getUpperBounds ().get (0 ));
230
+ }
231
+
232
+ if (classifier instanceof KType ktp ) {
233
+ return resolveClass (ktp );
234
+ }
235
+
236
+ throw new UnsupportedOperationException (String .format ("Unsupported KClassifier: %s" , classifier ));
200
237
}
201
238
202
239
private ValueBoxing (BoxingRules rules , KType type , KClass <?> kClass , boolean optional ) {
@@ -216,7 +253,7 @@ private ValueBoxing(BoxingRules rules, KType type, KClass<?> kClass, boolean opt
216
253
KClass <?> nestedClass ;
217
254
218
255
// bound flattening
219
- if (nestedType .getClassifier ()instanceof KTypeParameter ktp ) {
256
+ if (nestedType .getClassifier () instanceof KTypeParameter ktp ) {
220
257
nestedClass = getUpperBound (ktp );
221
258
} else {
222
259
nestedClass = (KClass <?>) nestedType .getClassifier ();
@@ -239,7 +276,7 @@ private static KClass<?> getUpperBound(KTypeParameter typeParameter) {
239
276
240
277
for (KType upperBound : typeParameter .getUpperBounds ()) {
241
278
242
- if (upperBound .getClassifier ()instanceof KClass <?> kc ) {
279
+ if (upperBound .getClassifier () instanceof KClass <?> kc ) {
243
280
return kc ;
244
281
}
245
282
}
@@ -249,11 +286,11 @@ private static KClass<?> getUpperBound(KTypeParameter typeParameter) {
249
286
250
287
static KType resolveType (KType type ) {
251
288
252
- if (type .getClassifier ()instanceof KTypeParameter ktp ) {
289
+ if (type .getClassifier () instanceof KTypeParameter ktp ) {
253
290
254
291
for (KType upperBound : ktp .getUpperBounds ()) {
255
292
256
- if (upperBound .getClassifier ()instanceof KClass <?> kc ) {
293
+ if (upperBound .getClassifier () instanceof KClass <?> kc ) {
257
294
return upperBound ;
258
295
}
259
296
}
0 commit comments