From 32536067f17d2066c86633376cf0e02864bbe7a7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 13 Oct 2017 17:31:58 +0200 Subject: [PATCH] DATACMNS-1197 - Polishing. Some cleanups in KorlinReflectionUtils. Removed compiler warnings about nullability and raw types. Original pull request: #254. --- .../data/util/ReflectionUtils.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/springframework/data/util/ReflectionUtils.java b/src/main/java/org/springframework/data/util/ReflectionUtils.java index 430661857..d8963b542 100644 --- a/src/main/java/org/springframework/data/util/ReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/ReflectionUtils.java @@ -23,7 +23,6 @@ import kotlin.reflect.KMutableProperty; import kotlin.reflect.KProperty; import kotlin.reflect.KType; import kotlin.reflect.jvm.ReflectJvmMapping; -import kotlin.reflect.jvm.internal.impl.load.kotlin.header.KotlinClassHeader; import kotlin.reflect.jvm.internal.impl.load.kotlin.header.KotlinClassHeader.Kind; import kotlin.reflect.jvm.internal.impl.load.kotlin.reflect.ReflectKotlinClass; import lombok.NonNull; @@ -384,13 +383,7 @@ public class ReflectionUtils { ReflectKotlinClass kotlinClass = ReflectKotlinClass.Factory.create(type); - if (kotlinClass == null) { - return false; - } - - KotlinClassHeader classHeader = kotlinClass.getClassHeader(); - - return classHeader.getKind() == Kind.CLASS; + return kotlinClass == null ? false : kotlinClass.getClassHeader().getKind() == Kind.CLASS; } /** @@ -428,19 +421,24 @@ public class ReflectionUtils { static boolean isNullable(MethodParameter parameter) { Method method = parameter.getMethod(); + + if (method == null) { + throw new IllegalStateException(String.format("Cannot obtain method from parameter %s!", parameter)); + } + KFunction kotlinFunction = ReflectJvmMapping.getKotlinFunction(method); if (kotlinFunction == null) { // Fallback to own lookup because there's no public Kotlin API for that kind of lookup until // https://youtrack.jetbrains.com/issue/KT-20768 gets resolved. - Optional first = findKFunction(method); - - kotlinFunction = first.orElseThrow( - () -> new IllegalArgumentException(String.format("Cannot resolve %s to a Kotlin function!", parameter))); + kotlinFunction = findKFunction(method)// + .orElseThrow(() -> new IllegalArgumentException( + String.format("Cannot resolve %s to a Kotlin function!", parameter))); } - KType type = parameter.getParameterIndex() == -1 ? kotlinFunction.getReturnType() + KType type = parameter.getParameterIndex() == -1 // + ? kotlinFunction.getReturnType() // : kotlinFunction.getParameters().get(parameter.getParameterIndex() + 1).getType(); return type.isMarkedNullable(); @@ -452,26 +450,22 @@ public class ReflectionUtils { * @param method the JVM {@link Method} to look up. * @return {@link Optional} wrapping a possibly existing {@link KFunction}. */ - private static Optional findKFunction(Method method) { + private static Optional> findKFunction(Method method) { KClass kotlinClass = JvmClassMappingKt.getKotlinClass(method.getDeclaringClass()); return kotlinClass.getMembers() // .stream() // .flatMap(KotlinReflectionUtils::toKFunctionStream) // - .filter(it -> { - - Method javaMethod = ReflectJvmMapping.getJavaMethod(it); - return javaMethod != null && javaMethod.equals(method); - }) // + .filter(it -> isSame(it, method)) // .findFirst(); } - private static Stream toKFunctionStream(KCallable it) { + private static Stream> toKFunctionStream(KCallable it) { if (it instanceof KMutableProperty) { - KMutableProperty property = (KMutableProperty) it; + KMutableProperty property = (KMutableProperty) it; return Stream.of(property.getGetter(), property.getSetter()); } @@ -487,5 +481,11 @@ public class ReflectionUtils { return Stream.empty(); } + + private static boolean isSame(KFunction function, Method method) { + + Method javaMethod = ReflectJvmMapping.getJavaMethod(function); + return javaMethod != null && javaMethod.equals(method); + } } }