Polish Kotlin nullable support

This commit polishes Kotlin nullable support by reusing
MethodParameter#isOptional() instead of adding a new
MethodParameter#isNullable() method, adds
Kotlin tests and introduces Spring Web Reactive
support.

Issue: SPR-14165
This commit is contained in:
Sebastien Deleuze
2016-11-21 23:59:41 +01:00
parent fada91e538
commit a143b57d4b
10 changed files with 609 additions and 63 deletions

View File

@@ -310,12 +310,12 @@ public class MethodParameter {
}
/**
* Return whether this method parameter is declared as optional
* in the form of Java 8's {@link java.util.Optional}.
* Return whether this method indicates a parameter which is not required
* (either in the form of Java 8's {@link java.util.Optional} or Kotlin nullable type).
* @since 4.3
*/
public boolean isOptional() {
return (getParameterType() == Optional.class);
return (getParameterType() == Optional.class || KotlinUtils.isNullable(this));
}
/**
@@ -327,18 +327,7 @@ public class MethodParameter {
* @see #nested()
*/
public MethodParameter nestedIfOptional() {
return (isOptional() ? nested() : this);
}
/**
* Return whether this method parameter is declared as a "nullable" value, if supported by
* the underlying language. Currently the only supported language is Kotlin.
* @since 5.0
*/
public boolean isNullable() {
return KotlinUtils.isKotlinPresent() &&
KotlinUtils.isKotlinClass(getContainingClass()) &&
KotlinUtils.isNullable(this.parameterIndex, this.method, this.constructor);
return (getParameterType() == Optional.class ? nested() : this);
}
/**

View File

@@ -22,7 +22,6 @@ import kotlin.reflect.KParameter;
import kotlin.reflect.jvm.ReflectJvmMapping;
import org.springframework.core.MethodParameter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
@@ -31,44 +30,55 @@ import java.util.stream.Collectors;
* Miscellaneous Kotlin utility methods.
*
* @author Raman Gupta
* @author Sebastien Deleuze
* @since 5.0
*/
public class KotlinUtils {
public abstract class KotlinUtils {
private static final boolean kotlinPresent;
static {
kotlinPresent = ClassUtils.isPresent("kotlin.Unit", MethodParameter.class.getClassLoader());
}
private static final boolean kotlinPresent = ClassUtils.isPresent("kotlin.Unit", KotlinUtils.class.getClassLoader());
/**
* Return whether Kotlin is available on the classpath or not.
*/
public static boolean isKotlinPresent() {
return kotlinPresent;
}
/**
* Return whether the specified type is a Kotlin class or not.
*/
public static boolean isKotlinClass(Class<?> type) {
return type != null && type.getDeclaredAnnotation(Metadata.class) != null;
Assert.notNull(type, "Type must not be null");
return isKotlinPresent() && type.getDeclaredAnnotation(Metadata.class) != null;
}
public static boolean isNullable(int parameterIndex, Method method, Constructor<?> constructor) {
if(parameterIndex < 0) {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return function != null && function.getReturnType().isMarkedNullable();
} else {
KFunction<?> function = method != null ?
ReflectJvmMapping.getKotlinFunction(method) :
ReflectJvmMapping.getKotlinFunction(constructor);
if(function != null) {
@SuppressWarnings("unchecked")
List<KParameter> parameters = function.getParameters();
return parameters
.stream()
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()))
.collect(Collectors.toList())
.get(parameterIndex)
.getType()
.isMarkedNullable();
/**
* Check whether the specified {@link MethodParameter} represents a nullable Kotlin type or not.
*/
public static boolean isNullable(MethodParameter methodParameter) {
Method method = methodParameter.getMethod();
int parameterIndex = methodParameter.getParameterIndex();
if (isKotlinClass(methodParameter.getContainingClass())) {
if (parameterIndex < 0) {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return function != null && function.getReturnType().isMarkedNullable();
}
else {
KFunction<?> function = (method != null ? ReflectJvmMapping.getKotlinFunction(method) :
ReflectJvmMapping.getKotlinFunction(methodParameter.getConstructor()));
if (function != null) {
List<KParameter> parameters = function.getParameters();
return parameters
.stream()
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()))
.collect(Collectors.toList())
.get(parameterIndex)
.getType()
.isMarkedNullable();
}
}
return false;
}
return false;
}
}

View File

@@ -11,20 +11,22 @@ import org.springframework.core.MethodParameter
import org.springframework.util.KotlinUtils.*
/**
* Unit tests for [KotlinUtils].
* Test fixture for [KotlinUtils].
*
* @author Raman Gupta
* @author Sebastien Deleuze
*/
class KotlinUtilsTests {
private lateinit var methodNullable: Method
private lateinit var methodNonNullable: Method
lateinit var nullableMethod: Method
lateinit var nonNullableMethod: Method
@Before
@Throws(NoSuchMethodException::class)
fun setUp() {
methodNullable = javaClass.getMethod("methodNullable", String::class.java, java.lang.Long.TYPE)
methodNonNullable = javaClass.getMethod("methodNonNullable", String::class.java, java.lang.Long.TYPE)
fun setup() {
nullableMethod = javaClass.getMethod("nullable", String::class.java)
nonNullableMethod = javaClass.getMethod("nonNullable", String::class.java)
}
@Test
@@ -35,26 +37,26 @@ class KotlinUtilsTests {
@Test
fun `Are kotlin classes detected`() {
assertFalse(isKotlinClass(null))
assertFalse(isKotlinClass(MethodParameter::class.java))
assertTrue(isKotlinClass(javaClass))
}
@Test
fun `Obtains method return type nullability`() {
assertTrue(isNullable(-1, methodNullable, null))
assertFalse(isNullable(-1, methodNonNullable, null))
assertTrue(isNullable(MethodParameter(nullableMethod, -1)))
assertFalse(isNullable(MethodParameter(nonNullableMethod, -1)))
}
@Test
fun `Obtains method parameter nullability`() {
assertTrue(isNullable(0, methodNullable, null))
assertFalse(isNullable(1, methodNullable, null))
assertTrue(isNullable(MethodParameter(nullableMethod, 0)))
assertFalse(isNullable(MethodParameter(nonNullableMethod, 0)))
}
@Suppress("unused", "unused_parameter")
fun methodNullable(p1: String?, p2: Long): Int? = 42
fun nullable(p1: String?): Int? = 42
@Suppress("unused", "unused_parameter")
fun methodNonNullable(p1: String?, p2: Long): Int = 42
fun nonNullable(p1: String): Int = 42
}