Browse Source

DATACMNS-1779 - Consider kotlin.Unit a simple type.

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.
2.2.x
Mark Paluch 5 years ago
parent
commit
cd901b93eb
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 2
      src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java
  2. 2
      src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java
  3. 23
      src/main/java/org/springframework/data/util/ReflectionUtils.java
  4. 11
      src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java

2
src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java

@ -157,7 +157,7 @@ public class SimpleTypeHolder {
String typeName = type.getName(); 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; return true;
} }

2
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) { private static boolean requiresNoValue(MethodParameter parameter) {
return parameter.getParameterType().equals(Void.class) || parameter.getParameterType().equals(Void.TYPE); return ReflectionUtils.isVoid(parameter.getParameterType());
} }
} }
} }

23
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. * A {@link FieldFilter} that has a description.
* *
@ -394,7 +415,7 @@ public class ReflectionUtils {
*/ */
public static boolean isNullable(MethodParameter parameter) { public static boolean isNullable(MethodParameter parameter) {
if (Void.class.equals(parameter.getParameterType()) || Void.TYPE.equals(parameter.getParameterType())) { if (isVoid(parameter.getParameterType())) {
return true; return true;
} }

11
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 static org.assertj.core.api.Assertions.*;
import kotlin.Unit;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -163,6 +165,15 @@ public class ReflectionUtilsUnitTests {
assertThat(ReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClassKt.class)).isFalse(); 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 { static class Sample {
public String field; public String field;

Loading…
Cancel
Save