Skip to content

Commit ea7477f

Browse files
authored
GH-10083: Add Nullability to Core router package
Related to: #10083
1 parent 6224ef9 commit ea7477f

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/RouterAnnotationPostProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.integration.router.MethodInvokingRouter;
3838
import org.springframework.integration.util.MessagingAnnotationUtils;
3939
import org.springframework.messaging.MessageHandler;
40+
import org.springframework.util.Assert;
4041
import org.springframework.util.ObjectUtils;
4142
import org.springframework.util.StringUtils;
4243

@@ -169,6 +170,7 @@ private void routerAttributes(List<Annotation> annotations, AbstractMessageRoute
169170
}
170171
Properties properties = (Properties) getConversionService().convert(mappings.toString(),
171172
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Properties.class));
173+
Assert.state(properties != null, "The properties for channel mappings must not be null.");
172174
methodInvokingRouter.replaceChannelMappings(properties);
173175
}
174176

spring-integration-core/src/main/java/org/springframework/integration/router/AbstractMappingMessageRouter.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Properties;
2828
import java.util.Set;
2929

30+
import org.jspecify.annotations.Nullable;
31+
3032
import org.springframework.core.convert.ConversionService;
3133
import org.springframework.core.log.LogMessage;
3234
import org.springframework.integration.support.management.MappingMessageRouterManagement;
@@ -50,6 +52,7 @@
5052
* @author Artem Bilan
5153
* @author Trung Pham
5254
* @author Ngoc Nhan
55+
* @author Glenn Renfro
5356
*
5457
* @since 2.1
5558
*/
@@ -72,9 +75,9 @@ protected boolean removeEldestEntry(Entry<String, MessageChannel> eldest) {
7275

7376
});
7477

75-
private String prefix;
78+
private @Nullable String prefix;
7679

77-
private String suffix;
80+
private @Nullable String suffix;
7881

7982
private boolean resolutionRequired = true;
8083

@@ -104,15 +107,15 @@ public void setChannelMappings(Map<String, String> channelMappings) {
104107
* Specify a prefix to be added to each channel name prior to resolution.
105108
* @param prefix The prefix.
106109
*/
107-
public void setPrefix(String prefix) {
110+
public void setPrefix(@Nullable String prefix) {
108111
this.prefix = prefix;
109112
}
110113

111114
/**
112115
* Specify a suffix to be added to each channel name prior to resolution.
113116
* @param suffix The suffix.
114117
*/
115-
public void setSuffix(String suffix) {
118+
public void setSuffix(@Nullable String suffix) {
116119
this.suffix = suffix;
117120
}
118121

@@ -274,7 +277,7 @@ protected Collection<MessageChannel> determineTargetChannels(Message<?> message)
274277
* @param message The message.
275278
* @return The channel keys.
276279
*/
277-
protected abstract List<Object> getChannelKeys(Message<?> message);
280+
protected abstract @Nullable List<Object> getChannelKeys(Message<?> message);
278281

279282
/**
280283
* Convenience method allowing conversion of a list
@@ -289,7 +292,6 @@ protected Collection<MessageChannel> determineTargetChannels(Message<?> message)
289292
@Override
290293
@ManagedOperation
291294
public void replaceChannelMappings(Properties channelMappings) {
292-
Assert.notNull(channelMappings, "'channelMappings' must not be null");
293295
Map<String, String> newChannelMappings = new LinkedHashMap<>();
294296
Set<String> keys = channelMappings.stringPropertyNames();
295297
for (String key : keys) {
@@ -304,7 +306,7 @@ private void doSetChannelMappings(Map<String, String> newChannelMappings) {
304306
logger.debug(LogMessage.format("Channel mappings: %s replaced with: %s", oldChannelMappings, newChannelMappings));
305307
}
306308

307-
private MessageChannel resolveChannelForName(String channelName, Message<?> message) {
309+
private @Nullable MessageChannel resolveChannelForName(String channelName, Message<?> message) {
308310
MessageChannel channel = null;
309311
try {
310312
channel = getChannelResolver().resolveDestination(channelName);
@@ -360,7 +362,7 @@ private void addChannel(Collection<MessageChannel> channels, Message<?> message,
360362
}
361363
}
362364

363-
private void addToCollection(Collection<MessageChannel> channels, Collection<?> channelKeys, Message<?> message) {
365+
private void addToCollection(Collection<MessageChannel> channels, @Nullable Collection<?> channelKeys, Message<?> message) {
364366
if (channelKeys == null) {
365367
return;
366368
}

spring-integration-core/src/main/java/org/springframework/integration/router/AbstractMessageRouter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.concurrent.locks.Lock;
2222
import java.util.concurrent.locks.ReentrantLock;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
import org.springframework.beans.factory.BeanFactory;
2527
import org.springframework.core.convert.ConversionService;
2628
import org.springframework.core.convert.support.DefaultConversionService;
@@ -47,6 +49,7 @@
4749
* @author Stefan Ferstl
4850
* @author Artem Bilan
4951
* @author Christian Tzolov
52+
* @author Glenn Renfro
5053
*/
5154
@ManagedResource
5255
@IntegrationManagedResource
@@ -56,9 +59,9 @@ public abstract class AbstractMessageRouter extends AbstractMessageHandler imple
5659

5760
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
5861

59-
private volatile MessageChannel defaultOutputChannel;
62+
private volatile @Nullable MessageChannel defaultOutputChannel;
6063

61-
private volatile String defaultOutputChannelName;
64+
private volatile @Nullable String defaultOutputChannelName;
6265

6366
private volatile boolean ignoreSendFailures;
6467

@@ -85,7 +88,7 @@ public void setDefaultOutputChannel(MessageChannel defaultOutputChannel) {
8588
* @since 4.3
8689
*/
8790
@Override
88-
public MessageChannel getDefaultOutputChannel() {
91+
public @Nullable MessageChannel getDefaultOutputChannel() {
8992
if (this.defaultOutputChannelName != null) {
9093
this.lock.lock();
9194
try {

spring-integration-core/src/main/java/org/springframework/integration/router/MessageRouter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.router;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.messaging.MessageChannel;
2022

2123
/**
@@ -31,6 +33,7 @@ public interface MessageRouter {
3133
* Get the default output channel.
3234
* @return the channel.
3335
*/
36+
@Nullable
3437
MessageChannel getDefaultOutputChannel();
3538

3639
}

spring-integration-core/src/main/java/org/springframework/integration/router/PayloadTypeRouter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.LinkedList;
2121
import java.util.List;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.messaging.Message;
2426
import org.springframework.util.CollectionUtils;
2527

@@ -31,6 +33,7 @@
3133
* @author Oleg Zhurakousky
3234
* @author Gary Russell
3335
* @author Artem Bilan
36+
* @author Glenn Renfro
3437
*/
3538
public class PayloadTypeRouter extends AbstractMappingMessageRouter {
3639

@@ -46,7 +49,7 @@ public class PayloadTypeRouter extends AbstractMappingMessageRouter {
4649
* preferring direct interface over indirect subclass
4750
*/
4851
@Override
49-
protected List<Object> getChannelKeys(Message<?> message) {
52+
protected @Nullable List<Object> getChannelKeys(Message<?> message) {
5053
if (CollectionUtils.isEmpty(getChannelMappings())) {
5154
return null;
5255
}
@@ -59,7 +62,7 @@ protected List<Object> getChannelKeys(Message<?> message) {
5962
return (closestMatch != null) ? Collections.singletonList(closestMatch) : null;
6063
}
6164

62-
private String findClosestMatch(Class<?> type, boolean isArray) { // NOSONAR
65+
private @Nullable String findClosestMatch(Class<?> type, boolean isArray) { // NOSONAR
6366
int minTypeDiffWeight = Integer.MAX_VALUE;
6467
List<String> matches = new LinkedList<>();
6568
for (String candidate : getChannelMappings().keySet()) {

spring-integration-core/src/main/java/org/springframework/integration/router/RecipientListRouter.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
* @author Artem Bilan
7777
* @author Liujiong
7878
* @author Gary Russell
79+
* @author Glenn Renfro
7980
*/
8081
public class RecipientListRouter extends AbstractMessageRouter implements RecipientListRouterManagement {
8182

@@ -151,11 +152,11 @@ public void addRecipient(String channelName) {
151152
addRecipient(channelName, (MessageSelector) null);
152153
}
153154

154-
public void addRecipient(String channelName, MessageSelector selector) {
155+
public void addRecipient(String channelName, @Nullable MessageSelector selector) {
155156
addRecipient(channelName, selector, this.recipients);
156157
}
157158

158-
private void addRecipient(String channelName, MessageSelector selector, Queue<Recipient> recipientsToAdd) {
159+
private void addRecipient(String channelName, @Nullable MessageSelector selector, Queue<Recipient> recipientsToAdd) {
159160
Assert.hasText(channelName, "'channelName' must not be empty.");
160161
Recipient recipient = new Recipient(channelName, selector);
161162
setupRecipient(recipient);
@@ -166,7 +167,7 @@ public void addRecipient(MessageChannel channel) {
166167
addRecipient(channel, null);
167168
}
168169

169-
public void addRecipient(MessageChannel channel, MessageSelector selector) {
170+
public void addRecipient(MessageChannel channel, @Nullable MessageSelector selector) {
170171
Recipient recipient = new Recipient(channel, selector);
171172
setupRecipient(recipient);
172173
this.recipients.add(recipient);
@@ -273,19 +274,19 @@ protected void onInit() {
273274

274275
public static class Recipient {
275276

276-
private final MessageSelector selector;
277+
private final @Nullable MessageSelector selector;
277278

278-
private MessageChannel channel;
279+
private @Nullable MessageChannel channel;
279280

280-
private String channelName;
281+
private @Nullable String channelName;
281282

282-
private DestinationResolver<MessageChannel> channelResolver;
283+
private @Nullable DestinationResolver<MessageChannel> channelResolver;
283284

284285
public Recipient(MessageChannel channel) {
285286
this(channel, null);
286287
}
287288

288-
public Recipient(MessageChannel channel, MessageSelector selector) {
289+
public Recipient(MessageChannel channel, @Nullable MessageSelector selector) {
289290
this.channel = channel;
290291
this.selector = selector;
291292
}
@@ -294,7 +295,7 @@ public Recipient(String channelName) {
294295
this(channelName, null);
295296
}
296297

297-
public Recipient(String channelName, MessageSelector selector) {
298+
public Recipient(String channelName, @Nullable MessageSelector selector) {
298299
this.channelName = channelName;
299300
this.selector = selector;
300301
}
@@ -303,7 +304,7 @@ public void setChannelResolver(DestinationResolver<MessageChannel> channelResolv
303304
this.channelResolver = channelResolver;
304305
}
305306

306-
private MessageSelector getSelector() {
307+
private @Nullable MessageSelector getSelector() {
307308
return this.selector;
308309
}
309310

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Provides classes supporting the router pattern.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.router;

0 commit comments

Comments
 (0)