Consistent use of @Nullable across the codebase (even for internals)

Beyond just formally declaring the current behavior, this revision actually enforces non-null behavior in selected signatures now, not tolerating null values anymore when not explicitly documented. It also changes some utility methods with historic null-in/null-out tolerance towards enforced non-null return values, making them a proper citizen in non-null assignments.

Some issues are left as to-do: in particular a thorough revision of spring-test, and a few tests with unclear failures (ignored as "TODO: NULLABLE") to be sorted out in a follow-up commit.

Issue: SPR-15540
This commit is contained in:
Juergen Hoeller
2017-06-07 14:17:48 +02:00
parent ffc3f6d87d
commit f813712f5b
1493 changed files with 10670 additions and 9172 deletions

View File

@@ -23,7 +23,10 @@ import java.io.InputStream;
import org.springframework.util.Assert;
/**
* @author Arjen Poutsma
* Mock implementation of {@link HttpInputMessage}.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
public class MockHttpInputMessage implements HttpInputMessage {
@@ -42,6 +45,7 @@ public class MockHttpInputMessage implements HttpInputMessage {
this.body = body;
}
@Override
public HttpHeaders getHeaders() {
return headers;
@@ -51,4 +55,5 @@ public class MockHttpInputMessage implements HttpInputMessage {
public InputStream getBody() throws IOException {
return body;
}
}

View File

@@ -87,14 +87,6 @@ public class Jackson2ObjectMapperBuilderTests {
private static final String DATE_FORMAT = "yyyy-MM-dd";
@Test
public void settersWithNullValues() {
// Should not crash:
Jackson2ObjectMapperBuilder.json().serializers((JsonSerializer<?>[]) null)
.serializersByType(null).deserializersByType(null)
.featuresToEnable((Object[]) null).featuresToDisable((Object[]) null);
}
@Test(expected = FatalBeanException.class)
public void unknownFeature() {
Jackson2ObjectMapperBuilder.json().featuresToEnable(Boolean.TRUE).build();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -87,15 +87,6 @@ public class Jackson2ObjectMapperFactoryBeanTests {
private final Jackson2ObjectMapperFactoryBean factory = new Jackson2ObjectMapperFactoryBean();
@Test
public void settingNullValuesShouldNotThrowExceptions() {
this.factory.setSerializers((JsonSerializer<?>[]) null);
this.factory.setSerializersByType(null);
this.factory.setDeserializersByType(null);
this.factory.setFeaturesToEnable((Object[]) null);
this.factory.setFeaturesToDisable((Object[]) null);
}
@Test(expected = FatalBeanException.class)
public void unknownFeature() {
this.factory.setFeaturesToEnable(Boolean.TRUE);

View File

@@ -42,6 +42,7 @@ import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.web.util.DefaultUriBuilderFactory;
import static org.junit.Assert.*;
@@ -294,7 +295,7 @@ public class RestTemplateTests {
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getHeaders()).willReturn(new HttpHeaders());
given(response.getBody()).willReturn(null);
given(response.getBody()).willReturn(StreamUtils.emptyInput());
Map<String, String> uriVariables = new HashMap<>(2);
uriVariables.put("hotel", "1");
@@ -526,8 +527,9 @@ public class RestTemplateTests {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(textPlain);
responseHeaders.setContentLength(10);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getBody()).willReturn(StreamUtils.emptyInput());
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
given(converter.read(Integer.class, response)).willReturn(null);
HttpStatus status = HttpStatus.OK;
@@ -554,8 +556,9 @@ public class RestTemplateTests {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(textPlain);
responseHeaders.setContentLength(10);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getBody()).willReturn(StreamUtils.emptyInput());
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
given(converter.read(Integer.class, response)).willReturn(null);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
@@ -652,8 +655,9 @@ public class RestTemplateTests {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(textPlain);
responseHeaders.setContentLength(10);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getBody()).willReturn(StreamUtils.emptyInput());
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
given(converter.read(Integer.class, response)).willReturn(null);
HttpStatus status = HttpStatus.OK;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,8 +47,6 @@ public class HtmlUtilsTests {
@Test
public void testEncodeIntoHtmlCharacterSet() {
assertNull("A null string should be converted to a null string",
HtmlUtils.htmlEscape(null));
assertEquals("An empty string should be converted to an empty string",
"", HtmlUtils.htmlEscape(""));
assertEquals("A string containing no special characters should not be affected",
@@ -75,8 +73,6 @@ public class HtmlUtilsTests {
@Test
public void testEncodeIntoHtmlCharacterSetFromUtf8() {
String utf8 = ("UTF-8");
assertNull("A null string should be converted to a null string",
HtmlUtils.htmlEscape(null, utf8));
assertEquals("An empty string should be converted to an empty string",
"", HtmlUtils.htmlEscape("", utf8));
assertEquals("A string containing no special characters should not be affected",
@@ -95,8 +91,6 @@ public class HtmlUtilsTests {
@Test
public void testDecodeFromHtmlCharacterSet() {
assertNull("A null string should be converted to a null string",
HtmlUtils.htmlUnescape(null));
assertEquals("An empty string should be converted to an empty string",
"", HtmlUtils.htmlUnescape(""));
assertEquals("A string containing no special characters should not be affected",