Nullability refinements and related polishing

This commit is contained in:
Juergen Hoeller 2020-10-13 00:08:15 +02:00
parent 1b63c31722
commit 7f365942a5
7 changed files with 37 additions and 15 deletions

View File

@ -77,14 +77,13 @@ public abstract class KotlinDetector {
/** /**
* Return {@code true} if the method is a suspending function. * Return {@code true} if the method is a suspending function.
*
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @since 5.3 * @since 5.3
*/ */
public static boolean isSuspendingFunction(Method method) { public static boolean isSuspendingFunction(Method method) {
if (KotlinDetector.isKotlinType(method.getDeclaringClass())) { if (KotlinDetector.isKotlinType(method.getDeclaringClass())) {
Class<?>[] types = method.getParameterTypes(); Class<?>[] types = method.getParameterTypes();
if ((types.length > 0) && "kotlin.coroutines.Continuation".equals(types[types.length - 1].getName())) { if (types.length > 0 && "kotlin.coroutines.Continuation".equals(types[types.length - 1].getName())) {
return true; return true;
} }
} }

View File

@ -27,6 +27,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextHierarchy; import org.springframework.test.context.ContextHierarchy;
@ -249,11 +250,11 @@ abstract class ContextLoaderUtils {
} }
private static void resolveContextConfigurationAttributes(List<ContextConfigurationAttributes> attributesList, private static void resolveContextConfigurationAttributes(List<ContextConfigurationAttributes> attributesList,
AnnotationDescriptor<ContextConfiguration> descriptor) { @Nullable AnnotationDescriptor<ContextConfiguration> descriptor) {
if (descriptor != null) { if (descriptor != null) {
convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(), convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(),
descriptor.getRootDeclaringClass(), attributesList); descriptor.getRootDeclaringClass(), attributesList);
resolveContextConfigurationAttributes(attributesList, descriptor.next()); resolveContextConfigurationAttributes(attributesList, descriptor.next());
} }
} }

View File

@ -163,6 +163,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
return findTransactionAttributeInEnclosingClassHierarchy(clazz); return findTransactionAttributeInEnclosingClassHierarchy(clazz);
} }
@Nullable
private TransactionAttribute findTransactionAttributeInEnclosingClassHierarchy(Class<?> clazz) { private TransactionAttribute findTransactionAttributeInEnclosingClassHierarchy(Class<?> clazz) {
if (MetaAnnotationUtils.searchEnclosingClass(clazz)) { if (MetaAnnotationUtils.searchEnclosingClass(clazz)) {
return findTransactionAttribute(clazz.getEnclosingClass()); return findTransactionAttribute(clazz.getEnclosingClass());

View File

@ -16,6 +16,7 @@
package org.springframework.test.context.web; package org.springframework.test.context.web;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextLoader; import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextBootstrapper; import org.springframework.test.context.TestContextBootstrapper;
@ -70,6 +71,7 @@ public class WebTestContextBootstrapper extends DefaultTestContextBootstrapper {
} }
} }
@Nullable
private static WebAppConfiguration getWebAppConfiguration(Class<?> testClass) { private static WebAppConfiguration getWebAppConfiguration(Class<?> testClass) {
return MetaAnnotationUtils.findMergedAnnotation(testClass, WebAppConfiguration.class); return MetaAnnotationUtils.findMergedAnnotation(testClass, WebAppConfiguration.class);
} }

View File

@ -31,6 +31,7 @@ import org.springframework.core.codec.AbstractDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
@ -58,6 +59,7 @@ public class KotlinSerializationJsonDecoder extends AbstractDecoder<Object> {
// String decoding needed for now, see https://github.com/Kotlin/kotlinx.serialization/issues/204 for more details // String decoding needed for now, see https://github.com/Kotlin/kotlinx.serialization/issues/204 for more details
private final StringDecoder stringDecoder = StringDecoder.allMimeTypes(StringDecoder.DEFAULT_DELIMITERS, false); private final StringDecoder stringDecoder = StringDecoder.allMimeTypes(StringDecoder.DEFAULT_DELIMITERS, false);
public KotlinSerializationJsonDecoder() { public KotlinSerializationJsonDecoder() {
this(Json.Default); this(Json.Default);
} }
@ -67,18 +69,23 @@ public class KotlinSerializationJsonDecoder extends AbstractDecoder<Object> {
this.json = json; this.json = json;
} }
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType) { public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
return super.canDecode(elementType, mimeType) && (!CharSequence.class.isAssignableFrom(elementType.toClass())); return (super.canDecode(elementType, mimeType) && !CharSequence.class.isAssignableFrom(elementType.toClass()));
} }
@Override @Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return Flux.error(new UnsupportedOperationException()); return Flux.error(new UnsupportedOperationException());
} }
@Override @Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return this.stringDecoder return this.stringDecoder
.decodeToMono(inputStream, elementType, mimeType, hints) .decodeToMono(inputStream, elementType, mimeType, hints)
.map(jsonText -> this.json.decodeFromString(serializer(elementType.getType()), jsonText)); .map(jsonText -> this.json.decodeFromString(serializer(elementType.getType()), jsonText));
@ -100,4 +107,5 @@ public class KotlinSerializationJsonDecoder extends AbstractDecoder<Object> {
} }
return serializer; return serializer;
} }
} }

View File

@ -34,6 +34,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent; import org.springframework.http.codec.ServerSentEvent;
import org.springframework.lang.Nullable;
import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
@ -57,6 +58,7 @@ public class KotlinSerializationJsonEncoder extends AbstractEncoder<Object> {
// CharSequence encoding needed for now, see https://github.com/Kotlin/kotlinx.serialization/issues/204 for more details // CharSequence encoding needed for now, see https://github.com/Kotlin/kotlinx.serialization/issues/204 for more details
private final CharSequenceEncoder charSequenceEncoder = CharSequenceEncoder.allMimeTypes(); private final CharSequenceEncoder charSequenceEncoder = CharSequenceEncoder.allMimeTypes();
public KotlinSerializationJsonEncoder() { public KotlinSerializationJsonEncoder() {
this(Json.Default); this(Json.Default);
} }
@ -66,15 +68,17 @@ public class KotlinSerializationJsonEncoder extends AbstractEncoder<Object> {
this.json = json; this.json = json;
} }
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType) { public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
return super.canEncode(elementType, mimeType) return (super.canEncode(elementType, mimeType) && !String.class.isAssignableFrom(elementType.toClass()) &&
&& (!String.class.isAssignableFrom(elementType.toClass())) !ServerSentEvent.class.isAssignableFrom(elementType.toClass()));
&& (!ServerSentEvent.class.isAssignableFrom(elementType.toClass()));
} }
@Override @Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory,
ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
if (inputStream instanceof Mono) { if (inputStream instanceof Mono) {
return Mono.from(inputStream) return Mono.from(inputStream)
.map(value -> encodeValue(value, bufferFactory, elementType, mimeType, hints)) .map(value -> encodeValue(value, bufferFactory, elementType, mimeType, hints))
@ -90,7 +94,9 @@ public class KotlinSerializationJsonEncoder extends AbstractEncoder<Object> {
} }
@Override @Override
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) { public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
String json = this.json.encodeToString(serializer(valueType.getType()), value); String json = this.json.encodeToString(serializer(valueType.getType()), value);
return this.charSequenceEncoder.encodeValue(json, bufferFactory, valueType, mimeType, null); return this.charSequenceEncoder.encodeValue(json, bufferFactory, valueType, mimeType, null);
} }
@ -111,4 +117,5 @@ public class KotlinSerializationJsonEncoder extends AbstractEncoder<Object> {
} }
return serializer; return serializer;
} }
} }

View File

@ -17,6 +17,7 @@
package org.springframework.web.servlet.mvc.method.annotation; package org.springframework.web.servlet.mvc.method.annotation;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@ -36,7 +37,10 @@ public class ContinuationHandlerMethodArgumentResolver implements HandlerMethodA
} }
@Override @Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
return null; return null;
} }
} }