Skip to content

Commit d1fc97e

Browse files
committed
DATACMNS-1197 - Polishing.
Some cleanups in KorlinReflectionUtils. Removed compiler warnings about nullability and raw types. Original pull request: #254.
1 parent 5cf4ba2 commit d1fc97e

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/main/java/org/springframework/data/util/ReflectionUtils.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import kotlin.reflect.KProperty;
2424
import kotlin.reflect.KType;
2525
import kotlin.reflect.jvm.ReflectJvmMapping;
26-
import kotlin.reflect.jvm.internal.impl.load.kotlin.header.KotlinClassHeader;
2726
import kotlin.reflect.jvm.internal.impl.load.kotlin.header.KotlinClassHeader.Kind;
2827
import kotlin.reflect.jvm.internal.impl.load.kotlin.reflect.ReflectKotlinClass;
2928
import lombok.NonNull;
@@ -384,13 +383,7 @@ public static boolean isSupportedKotlinClass(Class<?> type) {
384383

385384
ReflectKotlinClass kotlinClass = ReflectKotlinClass.Factory.create(type);
386385

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;
394387
}
395388

396389
/**
@@ -428,19 +421,24 @@ static class KotlinReflectionUtils {
428421
static boolean isNullable(MethodParameter parameter) {
429422

430423
Method method = parameter.getMethod();
424+
425+
if (method == null) {
426+
throw new IllegalStateException(String.format("Cannot obtain method from parameter %s!", parameter));
427+
}
428+
431429
KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(method);
432430

433431
if (kotlinFunction == null) {
434432

435433
// Fallback to own lookup because there's no public Kotlin API for that kind of lookup until
436434
// 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)));
441438
}
442439

443-
KType type = parameter.getParameterIndex() == -1 ? kotlinFunction.getReturnType()
440+
KType type = parameter.getParameterIndex() == -1 //
441+
? kotlinFunction.getReturnType() //
444442
: kotlinFunction.getParameters().get(parameter.getParameterIndex() + 1).getType();
445443

446444
return type.isMarkedNullable();
@@ -452,26 +450,22 @@ static boolean isNullable(MethodParameter parameter) {
452450
* @param method the JVM {@link Method} to look up.
453451
* @return {@link Optional} wrapping a possibly existing {@link KFunction}.
454452
*/
455-
private static Optional<? extends KFunction> findKFunction(Method method) {
453+
private static Optional<? extends KFunction<?>> findKFunction(Method method) {
456454

457455
KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(method.getDeclaringClass());
458456

459457
return kotlinClass.getMembers() //
460458
.stream() //
461459
.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)) //
467461
.findFirst();
468462
}
469463

470-
private static Stream<? extends KFunction> toKFunctionStream(KCallable<?> it) {
464+
private static Stream<? extends KFunction<?>> toKFunctionStream(KCallable<?> it) {
471465

472466
if (it instanceof KMutableProperty<?>) {
473467

474-
KMutableProperty property = (KMutableProperty<?>) it;
468+
KMutableProperty<?> property = (KMutableProperty<?>) it;
475469
return Stream.of(property.getGetter(), property.getSetter());
476470
}
477471

@@ -487,5 +481,11 @@ private static Stream<? extends KFunction> toKFunctionStream(KCallable<?> it) {
487481

488482
return Stream.empty();
489483
}
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+
}
490490
}
491491
}

0 commit comments

Comments
 (0)