DATACMNS-1197 - Polishing.

Some cleanups in KorlinReflectionUtils. Removed compiler warnings about nullability and raw types.

Original pull request: #254.
This commit is contained in:
Oliver Gierke
2017-10-13 17:31:58 +02:00
parent 634d8cbbd6
commit 32536067f1

View File

@@ -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<? extends KFunction> 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<? extends KFunction> findKFunction(Method method) {
private static Optional<? extends KFunction<?>> 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<? extends KFunction> toKFunctionStream(KCallable<?> it) {
private static Stream<? extends KFunction<?>> 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);
}
}
}