From cd901b93eba0bd663448fa79c55624c8c047084c 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 | 11 +++++++++ 4 files changed, 35 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 ca8927a9e..05e9e0b16 100644 --- a/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java +++ b/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java @@ -157,7 +157,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 88d9758ff..17920309d 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 @@ -158,7 +158,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()); } } } diff --git a/src/main/java/org/springframework/data/util/ReflectionUtils.java b/src/main/java/org/springframework/data/util/ReflectionUtils.java index d5dcfc6c2..b135714d1 100644 --- a/src/main/java/org/springframework/data/util/ReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/ReflectionUtils.java @@ -82,6 +82,27 @@ public 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.2.10 + */ + 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. * @@ -394,7 +415,7 @@ public 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 33bafa1f2..a5046dadd 100755 --- a/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java @@ -17,6 +17,8 @@ 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; @@ -163,6 +165,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;