Refine null-safety in more modules

This commit refines the null-safety in all remaining modules
except spring-test.

See gh-32475
This commit is contained in:
Sébastien Deleuze
2024-03-26 15:39:18 +01:00
parent 1b563f8ba4
commit 290a41d398
88 changed files with 172 additions and 72 deletions

View File

@@ -788,19 +788,19 @@ public final class ContentDisposition {
}
@Override
public Builder name(String name) {
public Builder name(@Nullable String name) {
this.name = name;
return this;
}
@Override
public Builder filename(String filename) {
public Builder filename(@Nullable String filename) {
this.filename = filename;
return this;
}
@Override
public Builder filename(String filename, Charset charset) {
public Builder filename(@Nullable String filename, @Nullable Charset charset) {
this.filename = filename;
this.charset = charset;
return this;
@@ -808,28 +808,28 @@ public final class ContentDisposition {
@Override
@SuppressWarnings("deprecation")
public Builder size(Long size) {
public Builder size(@Nullable Long size) {
this.size = size;
return this;
}
@Override
@SuppressWarnings("deprecation")
public Builder creationDate(ZonedDateTime creationDate) {
public Builder creationDate(@Nullable ZonedDateTime creationDate) {
this.creationDate = creationDate;
return this;
}
@Override
@SuppressWarnings("deprecation")
public Builder modificationDate(ZonedDateTime modificationDate) {
public Builder modificationDate(@Nullable ZonedDateTime modificationDate) {
this.modificationDate = modificationDate;
return this;
}
@Override
@SuppressWarnings("deprecation")
public Builder readDate(ZonedDateTime readDate) {
public Builder readDate(@Nullable ZonedDateTime readDate) {
this.readDate = readDate;
return this;
}

View File

@@ -301,7 +301,7 @@ public class ProblemDetail {
/**
* Create a {@code ProblemDetail} instance with the given status and detail.
*/
public static ProblemDetail forStatusAndDetail(HttpStatusCode status, String detail) {
public static ProblemDetail forStatusAndDetail(HttpStatusCode status, @Nullable String detail) {
Assert.notNull(status, "HttpStatusCode is required");
ProblemDetail problemDetail = forStatus(status.value());
problemDetail.setDetail(detail);

View File

@@ -54,6 +54,7 @@ class ReadOnlyHttpHeaders extends HttpHeaders {
@Override
@Nullable
public MediaType getContentType() {
if (this.cachedContentType != null) {
return this.cachedContentType;
@@ -83,6 +84,7 @@ class ReadOnlyHttpHeaders extends HttpHeaders {
}
@Override
@Nullable
public List<String> get(Object key) {
List<String> values = this.headers.get(key);
return (values != null ? Collections.unmodifiableList(values) : null);

View File

@@ -28,6 +28,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
@@ -53,7 +54,7 @@ public class Jackson2CborDecoder extends AbstractJackson2Decoder {
@Override
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
throw new UnsupportedOperationException("Does not support stream decoding yet");
}

View File

@@ -29,6 +29,7 @@ import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.AbstractJackson2Encoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
@@ -54,7 +55,8 @@ public class Jackson2CborEncoder extends AbstractJackson2Encoder {
@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) {
throw new UnsupportedOperationException("Does not support stream encoding yet");
}

View File

@@ -300,6 +300,7 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple
// Jackson2CodecSupport
@Override
@Nullable
protected <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType) {
return parameter.getParameterAnnotation(annotType);
}

View File

@@ -426,6 +426,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
// Jackson2CodecSupport
@Override
@Nullable
protected <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType) {
return parameter.getMethodAnnotation(annotType);
}

View File

@@ -82,6 +82,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
if (this.supportsReadStreaming && InputStreamResource.class == clazz) {
return new InputStreamResource(inputMessage.getBody()) {
@Override
@Nullable
public String getFilename() {
return inputMessage.getHeaders().getContentDisposition().getFilename();
}

View File

@@ -469,6 +469,7 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
@Override
@Nullable
public Class<?> getObjectType() {
return (this.objectMapper != null ? this.objectMapper.getClass() : null);
}

View File

@@ -22,6 +22,7 @@ import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeHintsRegistrar} implementation that registers reflection entries
@@ -38,7 +39,7 @@ class JacksonModulesRuntimeHints implements RuntimeHintsRegistrar {
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.reflection()
.registerTypeIfPresent(classLoader,
"com.fasterxml.jackson.datatype.jdk8.Jdk8Module", asJacksonModule)

View File

@@ -20,6 +20,7 @@ import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
/**
@@ -33,7 +34,7 @@ import org.springframework.util.ClassUtils;
class ProblemDetailRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
bindingRegistrar.registerReflectionHints(hints.reflection(), ProblemDetail.class);
if (ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader)) {

View File

@@ -61,6 +61,7 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse {
}
@Override
@Nullable
public HttpStatusCode getStatusCode() {
return getDelegate().getStatusCode();
}
@@ -71,6 +72,7 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse {
}
@Override
@Nullable
@Deprecated
public Integer getRawStatusCode() {
return getDelegate().getRawStatusCode();

View File

@@ -26,6 +26,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
@@ -70,6 +71,7 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
}
@Override
@Nullable
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
return this.restTemplate.exchange(newRequest(values), bodyType).getBody();
}

View File

@@ -144,6 +144,7 @@ public abstract class AbstractRefreshableWebApplicationContext extends AbstractR
}
@Override
@Nullable
public String[] getConfigLocations() {
return super.getConfigLocations();
}

View File

@@ -84,6 +84,7 @@ public abstract class AbstractMultipartHttpServletRequest extends HttpServletReq
}
@Override
@Nullable
public MultipartFile getFile(String name) {
return getMultipartFiles().getFirst(name);
}

View File

@@ -25,6 +25,7 @@ import java.util.stream.Collectors;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.validation.FieldError;
@@ -114,6 +115,7 @@ public abstract class BindErrorUtils {
}
@Override
@Nullable
protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale) {
String message = super.getDefaultMessage(resolvable, locale);
return (resolvable instanceof FieldError error ? error.getField() + ": " + message : message);

View File

@@ -19,6 +19,7 @@ package org.springframework.web.util;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.core.io.ClassPathResource;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeHintsRegistrar} implementation that registers resource
@@ -30,7 +31,7 @@ import org.springframework.core.io.ClassPathResource;
class WebUtilRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.resources().registerResource(
new ClassPathResource("HtmlCharacterEntityReferences.properties", getClass()));
}