From bb4d621a32f5521d000414218a3ee7bf52e77596 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 31 Jul 2020 10:49:52 +0200 Subject: [PATCH] DATACMNS-1779 - Consider kotlin.Unit a simple type. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kotlin.Unit is now considered a simple type to avoid PersistentEntity creation. ReflectionUtils.isVoid(…) now encapsulates the check so that calling code doesn't need to check if Kotlin is on the class path. --- .../data/mapping/model/SimpleTypeHolder.java | 2 +- .../support/MethodInvocationValidator.java | 2 +- .../data/util/ReflectionUtils.java | 23 ++++++++++++++++++- .../data/util/ReflectionUtilsUnitTests.java | 12 ++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java b/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java index effa9178c..72120967e 100644 --- a/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java +++ b/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java @@ -156,7 +156,7 @@ public class SimpleTypeHolder { String typeName = type.getName(); - if (typeName.startsWith("java.lang") || type.getName().startsWith("java.time")) { + if (typeName.startsWith("java.lang") || type.getName().startsWith("java.time") || typeName.equals("kotlin.Unit")) { return true; } diff --git a/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java b/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java index 7edd28556..b6de75bb8 100644 --- a/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java +++ b/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java @@ -160,7 +160,7 @@ public class MethodInvocationValidator implements MethodInterceptor { } private static boolean requiresNoValue(MethodParameter parameter) { - return parameter.getParameterType().equals(Void.class) || parameter.getParameterType().equals(Void.TYPE); + return ReflectionUtils.isVoid(parameter.getParameterType()); } public boolean[] getNullableParameters() { diff --git a/src/main/java/org/springframework/data/util/ReflectionUtils.java b/src/main/java/org/springframework/data/util/ReflectionUtils.java index 63b60d16d..323ff6a66 100644 --- a/src/main/java/org/springframework/data/util/ReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/ReflectionUtils.java @@ -70,6 +70,27 @@ public final class ReflectionUtils { } } + /** + * Check whether the given {@code type} represents a void type such as {@code void}, {@link Void} or Kotlin + * {@code Unit}. + * + * @param type must not be {@literal null}. + * @return whether the given the type is a void type. + * @since 2.4 + */ + public static boolean isVoid(Class type) { + + if (type == Void.class || Void.TYPE == type) { + return true; + } + + if (type.getName().equals("kotlin.Unit")) { + return true; + } + + return false; + } + /** * A {@link FieldFilter} that has a description. * @@ -399,7 +420,7 @@ public final class ReflectionUtils { */ public static boolean isNullable(MethodParameter parameter) { - if (Void.class.equals(parameter.getParameterType()) || Void.TYPE.equals(parameter.getParameterType())) { + if (isVoid(parameter.getParameterType())) { return true; } diff --git a/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java b/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java index f0b0497ff..a1ee9b763 100755 --- a/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java @@ -17,11 +17,14 @@ package org.springframework.data.util; import static org.assertj.core.api.Assertions.*; +import kotlin.Unit; + import java.lang.reflect.Constructor; import java.lang.reflect.Field; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.MethodParameter; import org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt; @@ -163,6 +166,15 @@ public class ReflectionUtilsUnitTests { assertThat(ReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClassKt.class)).isFalse(); } + @Test // DATACMNS-1779 + public void shouldReportIsVoid() { + + assertThat(ReflectionUtils.isVoid(Void.class)).isTrue(); + assertThat(ReflectionUtils.isVoid(Void.TYPE)).isTrue(); + assertThat(ReflectionUtils.isVoid(Unit.class)).isTrue(); + assertThat(ReflectionUtils.isVoid(String.class)).isFalse(); + } + static class Sample { public String field;