Perform NullAway build-time checks in more modules

This commit enables null-safety build-time checks in
all remaining modules except spring-test.

See gh-32475
This commit is contained in:
Sébastien Deleuze
2024-03-26 15:53:01 +01:00
parent 2fea3d7921
commit 8b51b36729
53 changed files with 93 additions and 31 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.http;
import org.springframework.lang.Nullable;
import org.springframework.util.InvalidMimeTypeException;
/**
@@ -36,8 +37,8 @@ public class InvalidMediaTypeException extends IllegalArgumentException {
* @param mediaType the offending media type
* @param message a detail message indicating the invalid part
*/
public InvalidMediaTypeException(String mediaType, String message) {
super("Invalid media type \"" + mediaType + "\": " + message);
public InvalidMediaTypeException(String mediaType, @Nullable String message) {
super(message != null ? "Invalid media type \"" + mediaType + "\": " + message : "Invalid media type \"" + mediaType);
this.mediaType = mediaType;
}

View File

@@ -205,8 +205,8 @@ public class RequestEntity<T> extends HttpEntity<T> {
if (!super.equals(other)) {
return false;
}
RequestEntity<?> otherEntity = (RequestEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.method, otherEntity.method) &&
return (other instanceof RequestEntity<?> otherEntity &&
ObjectUtils.nullSafeEquals(this.method, otherEntity.method) &&
ObjectUtils.nullSafeEquals(this.url, otherEntity.url));
}
@@ -736,8 +736,8 @@ public class RequestEntity<T> extends HttpEntity<T> {
if (!super.equals(other)) {
return false;
}
UriTemplateRequestEntity<?> otherEntity = (UriTemplateRequestEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.uriTemplate, otherEntity.uriTemplate) &&
return (other instanceof UriTemplateRequestEntity<?> otherEntity &&
ObjectUtils.nullSafeEquals(this.uriTemplate, otherEntity.uriTemplate) &&
ObjectUtils.nullSafeEquals(this.uriVarsArray, otherEntity.uriVarsArray) &&
ObjectUtils.nullSafeEquals(this.uriVarsMap, otherEntity.uriVarsMap));
}

View File

@@ -162,8 +162,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
if (!super.equals(other)) {
return false;
}
ResponseEntity<?> otherEntity = (ResponseEntity<?>) other;
return ObjectUtils.nullSafeEquals(this.status, otherEntity.status);
return (other instanceof ResponseEntity<?> otherEntity && ObjectUtils.nullSafeEquals(this.status, otherEntity.status));
}
@Override

View File

@@ -63,6 +63,7 @@ abstract class AbstractStreamingClientHttpRequest extends AbstractClientHttpRequ
}
@Override
@SuppressWarnings("NullAway")
protected final ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
if (this.body == null && this.bodyStream != null) {
this.body = outputStream -> this.bodyStream.writeTo(outputStream);

View File

@@ -90,6 +90,7 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
@Override
@SuppressWarnings("NullAway")
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
try {
HttpRequest request = buildRequest(headers, body);

View File

@@ -69,6 +69,7 @@ class JettyClientHttpRequest extends AbstractStreamingClientHttpRequest {
}
@Override
@SuppressWarnings("NullAway")
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
if (!headers.isEmpty()) {
this.request.headers(httpFields -> {

View File

@@ -138,6 +138,7 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
}
@Nullable
@SuppressWarnings("NullAway")
private Object buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap,
Map<String, Object> hints) {

View File

@@ -49,6 +49,7 @@ import org.springframework.lang.Nullable;
* @author Arjen Poutsma
* @since 5.3
*/
@SuppressWarnings("NullAway")
final class MultipartParser extends BaseSubscriber<DataBuffer> {
private static final byte CR = '\r';
@@ -115,6 +116,7 @@ final class MultipartParser extends BaseSubscriber<DataBuffer> {
}
@Override
@SuppressWarnings("NullAway")
protected void hookOnNext(DataBuffer value) {
this.requestOutstanding.set(false);
this.state.get().onNext(value);

View File

@@ -57,6 +57,7 @@ import org.springframework.util.FastByteArrayOutputStream;
* @author Arjen Poutsma
* @since 5.3
*/
@SuppressWarnings("NullAway")
final class PartGenerator extends BaseSubscriber<MultipartParser.Token> {
private static final Log logger = LogFactory.getLog(PartGenerator.class);

View File

@@ -167,6 +167,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
}
@SuppressWarnings("NullAway")
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
HttpOutputMessage outputMessage) throws IOException {

View File

@@ -329,7 +329,8 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
}
// Do not log warning for serializer not found (note: different message wording on Jackson 2.9)
boolean debugLevel = (cause instanceof JsonMappingException && cause.getMessage().startsWith("Cannot find"));
boolean debugLevel = (cause instanceof JsonMappingException && cause.getMessage() != null &&
cause.getMessage().startsWith("Cannot find"));
if (debugLevel ? logger.isDebugEnabled() : logger.isWarnEnabled()) {
String msg = "Failed to evaluate Jackson " + (type instanceof JavaType ? "de" : "") +

View File

@@ -47,6 +47,7 @@ import org.springframework.util.Assert;
* @since 5.0
* @param <T> the type of element signaled
*/
@SuppressWarnings("NullAway")
public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
/**

View File

@@ -40,6 +40,7 @@ import org.springframework.util.Assert;
* @since 5.0
* @param <T> the type of element signaled to the {@link Subscriber}
*/
@SuppressWarnings("NullAway")
public abstract class AbstractListenerWriteFlushProcessor<T> implements Processor<Publisher<? extends T>, Void> {
/**

View File

@@ -43,6 +43,7 @@ import org.springframework.util.StringUtils;
* @since 5.0
* @param <T> the type of element signaled to the {@link Subscriber}
*/
@SuppressWarnings("NullAway")
public abstract class AbstractListenerWriteProcessor<T> implements Processor<T, Void> {
/**

View File

@@ -131,6 +131,7 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
return new URI(url.toString());
}
@SuppressWarnings("NullAway")
private static MultiValueMap<String, String> initHeaders(
MultiValueMap<String, String> headerValues, HttpServletRequest request) {

View File

@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
* @author Rossen Stoyanchev
* @since 5.0
*/
@SuppressWarnings("NullAway")
class WriteResultPublisher implements Publisher<Void> {
/**

View File

@@ -489,7 +489,9 @@ final class HttpServiceMethod {
return request -> client.exchangeForEntityFlux(request, bodyType)
.map(entity -> {
Object body = reactiveAdapter.fromPublisher(entity.getBody());
Flux<?> entityBody = entity.getBody();
Assert.state(entityBody != null, "Entity body must not be null");
Object body = reactiveAdapter.fromPublisher(entityBody);
return new ResponseEntity<>(body, entity.getHeaders(), entity.getStatusCode());
});
}