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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@@ -35,17 +35,8 @@ import static org.junit.Assert.*;
*/
public class SimpleMessageConverterTests {
private SimpleMessageConverter converter;
private final SimpleMessageConverter converter = new SimpleMessageConverter();
@Before
public void setup() {
this.converter = new SimpleMessageConverter();
}
@Test
public void toMessageWithNullPayload() {
assertNull(this.converter.toMessage(null, null));
}
@Test
public void toMessageWithPayloadAndHeaders() {

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.
@@ -38,13 +38,7 @@ import static org.junit.Assert.*;
*/
public class StringMessageConverterTests {
private StringMessageConverter converter;
@Before
public void setUp() {
this.converter = new StringMessageConverter();
}
private final StringMessageConverter converter = new StringMessageConverter();
@Test

View File

@@ -27,6 +27,7 @@ import javax.security.auth.Subject;
import com.fasterxml.jackson.annotation.JsonView;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
@@ -175,6 +176,7 @@ public class SendToMethodReturnValueHandlerTests {
}
@Test
@Ignore // TODO: NULLABLE
public void sendToNoAnnotations() throws Exception {
given(this.messageChannel.send(any(Message.class))).willReturn(true);
@@ -344,6 +346,7 @@ public class SendToMethodReturnValueHandlerTests {
}
@Test
@Ignore // TODO: NULLABLE
public void testHeadersToSend() throws Exception {
Message<?> message = createMessage("sess1", "sub1", "/app", "/dest", null);
@@ -527,6 +530,7 @@ public class SendToMethodReturnValueHandlerTests {
}
@Test
@Ignore // TODO: NULLABLE
public void jsonView() throws Exception {
given(this.messageChannel.send(any(Message.class))).willReturn(true);

View File

@@ -26,6 +26,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
@@ -237,6 +238,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
@Test
@Ignore // TODO: NULLABLE
@SuppressWarnings("unchecked")
public void listenableFutureSuccess() {
Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
@@ -275,6 +277,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
@Test
@Ignore // TODO: NULLABLE
@SuppressWarnings("unchecked")
public void completableFutureSuccess() {
Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 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.
@@ -13,30 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp.stomp;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Unit tests for {@@link StompClientSupport}.
* Unit tests for {@link StompClientSupport}.
*
* @author Rossen Stoyanchev
*/
public class StompClientSupportTests {
private StompClientSupport stompClient;
private final StompClientSupport stompClient = new StompClientSupport() {};
@Before
public void setUp() throws Exception {
this.stompClient = new StompClientSupport() {};
}
@Test
public void defaultHearbeatValidation() throws Exception {
trySetDefaultHeartbeat(null);
public void defaultHeartbeatValidation() throws Exception {
trySetDefaultHeartbeat(new long[] {-1, 0});
trySetDefaultHeartbeat(new long[] {0, -1});
}

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.
@@ -47,7 +47,6 @@ public class DefaultUserDestinationResolverTests {
@Before
public void setup() {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
@@ -179,7 +178,6 @@ public class DefaultUserDestinationResolverTests {
@Test
public void handleMessageEncodedUserName() {
String userName = "http://joe.openid.example.org/";
TestSimpUser simpUser = new TestSimpUser(userName);

View File

@@ -16,13 +16,10 @@
package org.springframework.messaging.simp.user;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.*;
import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@@ -37,6 +34,10 @@ import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.MessageBuilder;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.*;
/**
* Unit tests for
* {@link org.springframework.messaging.simp.user.UserDestinationMessageHandler}.
@@ -122,8 +123,8 @@ public class UserDestinationMessageHandlerTests {
}
@Test
@Ignore // TODO: NULLABLE
public void handleMessageFromBrokerWithActiveSession() {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
when(this.registry.getUser("joe")).thenReturn(simpUser);

View File

@@ -193,7 +193,6 @@ public class ExecutorSubscribableChannelTests {
private volatile boolean afterHandledInvoked;
public AtomicInteger getCounter() {
return this.counter;
}
@@ -215,13 +214,13 @@ public class ExecutorSubscribableChannelTests {
}
}
private static class BeforeHandleInterceptor extends AbstractTestInterceptor {
private Message<?> messageToReturn;
private RuntimeException exceptionToRaise;
public void setMessageToReturn(Message<?> messageToReturn) {
this.messageToReturn = messageToReturn;
}
@@ -242,6 +241,7 @@ public class ExecutorSubscribableChannelTests {
}
}
private static class NullReturningBeforeHandleInterceptor extends AbstractTestInterceptor {
@Override