Skip to content

DATACMNS-1197 - Resolve Kotlin interface properties for nullability inspection. #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATACMNS-1197-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
77 changes: 74 additions & 3 deletions src/main/java/org/springframework/data/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
*/
package org.springframework.data.util;

import kotlin.jvm.JvmClassMappingKt;
import kotlin.reflect.KCallable;
import kotlin.reflect.KClass;
import kotlin.reflect.KFunction;
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;
Expand Down Expand Up @@ -402,11 +407,37 @@ public static boolean isNullable(MethodParameter parameter) {
}

if (isSupportedKotlinClass(parameter.getDeclaringClass())) {
return KotlinReflectionUtils.isNullable(parameter);
}

return !parameter.getParameterType().isPrimitive();
}

/**
* Reflection utility methods specific to Kotlin reflection.
*/
static class KotlinReflectionUtils {

/**
* Returns {@literal} whether the given {@link MethodParameter} is nullable. Its declaring method can reference a
* Kotlin function, property or interface property.
*
* @return {@literal true} if {@link MethodParameter} is nullable.
* @since 2.0.1
*/
static boolean isNullable(MethodParameter parameter) {

KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(parameter.getMethod());
Method method = parameter.getMethod();
KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(method);

if (kotlinFunction == null) {
throw new IllegalArgumentException(String.format("Cannot resolve %s to a Kotlin function!", parameter));

// 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)));
}

KType type = parameter.getParameterIndex() == -1 ? kotlinFunction.getReturnType()
Expand All @@ -415,6 +446,46 @@ public static boolean isNullable(MethodParameter parameter) {
return type.isMarkedNullable();
}

return !parameter.getParameterType().isPrimitive();
/**
* Lookup a {@link Method} to a {@link KFunction}.
*
* @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) {

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);
}) //
.findFirst();
}

private static Stream<? extends KFunction> toKFunctionStream(KCallable<?> it) {

if (it instanceof KMutableProperty<?>) {

KMutableProperty property = (KMutableProperty<?>) it;
return Stream.of(property.getGetter(), property.getSetter());
}

if (it instanceof KProperty<?>) {

KProperty<?> property = (KProperty<?>) it;
return Stream.of(property.getGetter());
}

if (it instanceof KFunction<?>) {
return Stream.of((KFunction<?>) it);
}

return Stream.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ public void considersRequiredKotlinNullableParameter() {
assertThat(repository.findByOptionalId(null)).isNull();
}

@Test // DATACMNS-1197
public void considersNullabilityForKotlinInterfaceProperties() {

KotlinUserRepository repository = factory.getRepository(KotlinUserRepository.class);

assertThatThrownBy(repository::getFindRouteQuery).isInstanceOf(EmptyResultDataAccessException.class);
}

private ConvertingRepository prepareConvertingRepository(final Object expectedValue) {

when(factory.queryOne.execute(Mockito.any(Object[].class))).then(invocation -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ interface KotlinUserRepository : Repository<User, String> {
fun findById(username: String): User

fun findByOptionalId(username: String?): User?

val findRouteQuery: String
}