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