23
23
import kotlin .reflect .KProperty ;
24
24
import kotlin .reflect .KType ;
25
25
import kotlin .reflect .jvm .ReflectJvmMapping ;
26
- import kotlin .reflect .jvm .internal .impl .load .kotlin .header .KotlinClassHeader ;
27
26
import kotlin .reflect .jvm .internal .impl .load .kotlin .header .KotlinClassHeader .Kind ;
28
27
import kotlin .reflect .jvm .internal .impl .load .kotlin .reflect .ReflectKotlinClass ;
29
28
import lombok .NonNull ;
@@ -384,13 +383,7 @@ public static boolean isSupportedKotlinClass(Class<?> type) {
384
383
385
384
ReflectKotlinClass kotlinClass = ReflectKotlinClass .Factory .create (type );
386
385
387
- if (kotlinClass == null ) {
388
- return false ;
389
- }
390
-
391
- KotlinClassHeader classHeader = kotlinClass .getClassHeader ();
392
-
393
- return classHeader .getKind () == Kind .CLASS ;
386
+ return kotlinClass == null ? false : kotlinClass .getClassHeader ().getKind () == Kind .CLASS ;
394
387
}
395
388
396
389
/**
@@ -428,19 +421,24 @@ static class KotlinReflectionUtils {
428
421
static boolean isNullable (MethodParameter parameter ) {
429
422
430
423
Method method = parameter .getMethod ();
424
+
425
+ if (method == null ) {
426
+ throw new IllegalStateException (String .format ("Cannot obtain method from parameter %s!" , parameter ));
427
+ }
428
+
431
429
KFunction <?> kotlinFunction = ReflectJvmMapping .getKotlinFunction (method );
432
430
433
431
if (kotlinFunction == null ) {
434
432
435
433
// Fallback to own lookup because there's no public Kotlin API for that kind of lookup until
436
434
// https://youtrack.jetbrains.com/issue/KT-20768 gets resolved.
437
- Optional <? extends KFunction > first = findKFunction (method );
438
-
439
- kotlinFunction = first .orElseThrow (
440
- () -> new IllegalArgumentException (String .format ("Cannot resolve %s to a Kotlin function!" , parameter )));
435
+ kotlinFunction = findKFunction (method )//
436
+ .orElseThrow (() -> new IllegalArgumentException (
437
+ String .format ("Cannot resolve %s to a Kotlin function!" , parameter )));
441
438
}
442
439
443
- KType type = parameter .getParameterIndex () == -1 ? kotlinFunction .getReturnType ()
440
+ KType type = parameter .getParameterIndex () == -1 //
441
+ ? kotlinFunction .getReturnType () //
444
442
: kotlinFunction .getParameters ().get (parameter .getParameterIndex () + 1 ).getType ();
445
443
446
444
return type .isMarkedNullable ();
@@ -452,26 +450,22 @@ static boolean isNullable(MethodParameter parameter) {
452
450
* @param method the JVM {@link Method} to look up.
453
451
* @return {@link Optional} wrapping a possibly existing {@link KFunction}.
454
452
*/
455
- private static Optional <? extends KFunction > findKFunction (Method method ) {
453
+ private static Optional <? extends KFunction <?> > findKFunction (Method method ) {
456
454
457
455
KClass <?> kotlinClass = JvmClassMappingKt .getKotlinClass (method .getDeclaringClass ());
458
456
459
457
return kotlinClass .getMembers () //
460
458
.stream () //
461
459
.flatMap (KotlinReflectionUtils ::toKFunctionStream ) //
462
- .filter (it -> {
463
-
464
- Method javaMethod = ReflectJvmMapping .getJavaMethod (it );
465
- return javaMethod != null && javaMethod .equals (method );
466
- }) //
460
+ .filter (it -> isSame (it , method )) //
467
461
.findFirst ();
468
462
}
469
463
470
- private static Stream <? extends KFunction > toKFunctionStream (KCallable <?> it ) {
464
+ private static Stream <? extends KFunction <?> > toKFunctionStream (KCallable <?> it ) {
471
465
472
466
if (it instanceof KMutableProperty <?>) {
473
467
474
- KMutableProperty property = (KMutableProperty <?>) it ;
468
+ KMutableProperty <?> property = (KMutableProperty <?>) it ;
475
469
return Stream .of (property .getGetter (), property .getSetter ());
476
470
}
477
471
@@ -487,5 +481,11 @@ private static Stream<? extends KFunction> toKFunctionStream(KCallable<?> it) {
487
481
488
482
return Stream .empty ();
489
483
}
484
+
485
+ private static boolean isSame (KFunction <?> function , Method method ) {
486
+
487
+ Method javaMethod = ReflectJvmMapping .getJavaMethod (function );
488
+ return javaMethod != null && javaMethod .equals (method );
489
+ }
490
490
}
491
491
}
0 commit comments