Skip to content

Commit 30e7d1e

Browse files
committed
Add nullability annotations to module/spring-boot-kafka
See gh-46587
1 parent d878ed5 commit 30e7d1e

14 files changed

+265
-253
lines changed

module/spring-boot-kafka/src/main/java/org/springframework/boot/kafka/autoconfigure/ConcurrentKafkaListenerContainerFactoryConfigurer.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.time.Duration;
2020
import java.util.function.Function;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.boot.context.properties.PropertyMapper;
2325
import org.springframework.boot.kafka.autoconfigure.KafkaProperties.Listener;
2426
import org.springframework.core.task.SimpleAsyncTaskExecutor;
@@ -36,6 +38,7 @@
3638
import org.springframework.kafka.support.converter.BatchMessageConverter;
3739
import org.springframework.kafka.support.converter.RecordMessageConverter;
3840
import org.springframework.kafka.transaction.KafkaAwareTransactionManager;
41+
import org.springframework.util.Assert;
3942

4043
/**
4144
* Configure {@link ConcurrentKafkaListenerContainerFactory} with sensible defaults tuned
@@ -53,133 +56,133 @@
5356
*/
5457
public class ConcurrentKafkaListenerContainerFactoryConfigurer {
5558

56-
private KafkaProperties properties;
59+
private @Nullable KafkaProperties properties;
5760

58-
private BatchMessageConverter batchMessageConverter;
61+
private @Nullable BatchMessageConverter batchMessageConverter;
5962

60-
private RecordMessageConverter recordMessageConverter;
63+
private @Nullable RecordMessageConverter recordMessageConverter;
6164

62-
private RecordFilterStrategy<Object, Object> recordFilterStrategy;
65+
private @Nullable RecordFilterStrategy<Object, Object> recordFilterStrategy;
6366

64-
private KafkaTemplate<Object, Object> replyTemplate;
67+
private @Nullable KafkaTemplate<Object, Object> replyTemplate;
6568

66-
private KafkaAwareTransactionManager<Object, Object> transactionManager;
69+
private @Nullable KafkaAwareTransactionManager<Object, Object> transactionManager;
6770

68-
private ConsumerAwareRebalanceListener rebalanceListener;
71+
private @Nullable ConsumerAwareRebalanceListener rebalanceListener;
6972

70-
private CommonErrorHandler commonErrorHandler;
73+
private @Nullable CommonErrorHandler commonErrorHandler;
7174

72-
private AfterRollbackProcessor<Object, Object> afterRollbackProcessor;
75+
private @Nullable AfterRollbackProcessor<Object, Object> afterRollbackProcessor;
7376

74-
private RecordInterceptor<Object, Object> recordInterceptor;
77+
private @Nullable RecordInterceptor<Object, Object> recordInterceptor;
7578

76-
private BatchInterceptor<Object, Object> batchInterceptor;
79+
private @Nullable BatchInterceptor<Object, Object> batchInterceptor;
7780

78-
private Function<MessageListenerContainer, String> threadNameSupplier;
81+
private @Nullable Function<MessageListenerContainer, String> threadNameSupplier;
7982

80-
private SimpleAsyncTaskExecutor listenerTaskExecutor;
83+
private @Nullable SimpleAsyncTaskExecutor listenerTaskExecutor;
8184

8285
/**
8386
* Set the {@link KafkaProperties} to use.
8487
* @param properties the properties
8588
*/
86-
void setKafkaProperties(KafkaProperties properties) {
89+
void setKafkaProperties(@Nullable KafkaProperties properties) {
8790
this.properties = properties;
8891
}
8992

9093
/**
9194
* Set the {@link BatchMessageConverter} to use.
9295
* @param batchMessageConverter the message converter
9396
*/
94-
void setBatchMessageConverter(BatchMessageConverter batchMessageConverter) {
97+
void setBatchMessageConverter(@Nullable BatchMessageConverter batchMessageConverter) {
9598
this.batchMessageConverter = batchMessageConverter;
9699
}
97100

98101
/**
99102
* Set the {@link RecordMessageConverter} to use.
100103
* @param recordMessageConverter the message converter
101104
*/
102-
void setRecordMessageConverter(RecordMessageConverter recordMessageConverter) {
105+
void setRecordMessageConverter(@Nullable RecordMessageConverter recordMessageConverter) {
103106
this.recordMessageConverter = recordMessageConverter;
104107
}
105108

106109
/**
107110
* Set the {@link RecordFilterStrategy} to use to filter incoming records.
108111
* @param recordFilterStrategy the record filter strategy
109112
*/
110-
void setRecordFilterStrategy(RecordFilterStrategy<Object, Object> recordFilterStrategy) {
113+
void setRecordFilterStrategy(@Nullable RecordFilterStrategy<Object, Object> recordFilterStrategy) {
111114
this.recordFilterStrategy = recordFilterStrategy;
112115
}
113116

114117
/**
115118
* Set the {@link KafkaTemplate} to use to send replies.
116119
* @param replyTemplate the reply template
117120
*/
118-
void setReplyTemplate(KafkaTemplate<Object, Object> replyTemplate) {
121+
void setReplyTemplate(@Nullable KafkaTemplate<Object, Object> replyTemplate) {
119122
this.replyTemplate = replyTemplate;
120123
}
121124

122125
/**
123126
* Set the {@link KafkaAwareTransactionManager} to use.
124127
* @param transactionManager the transaction manager
125128
*/
126-
void setTransactionManager(KafkaAwareTransactionManager<Object, Object> transactionManager) {
129+
void setTransactionManager(@Nullable KafkaAwareTransactionManager<Object, Object> transactionManager) {
127130
this.transactionManager = transactionManager;
128131
}
129132

130133
/**
131134
* Set the {@link ConsumerAwareRebalanceListener} to use.
132135
* @param rebalanceListener the rebalance listener.
133136
*/
134-
void setRebalanceListener(ConsumerAwareRebalanceListener rebalanceListener) {
137+
void setRebalanceListener(@Nullable ConsumerAwareRebalanceListener rebalanceListener) {
135138
this.rebalanceListener = rebalanceListener;
136139
}
137140

138141
/**
139142
* Set the {@link CommonErrorHandler} to use.
140143
* @param commonErrorHandler the error handler.
141144
*/
142-
public void setCommonErrorHandler(CommonErrorHandler commonErrorHandler) {
145+
public void setCommonErrorHandler(@Nullable CommonErrorHandler commonErrorHandler) {
143146
this.commonErrorHandler = commonErrorHandler;
144147
}
145148

146149
/**
147150
* Set the {@link AfterRollbackProcessor} to use.
148151
* @param afterRollbackProcessor the after rollback processor
149152
*/
150-
void setAfterRollbackProcessor(AfterRollbackProcessor<Object, Object> afterRollbackProcessor) {
153+
void setAfterRollbackProcessor(@Nullable AfterRollbackProcessor<Object, Object> afterRollbackProcessor) {
151154
this.afterRollbackProcessor = afterRollbackProcessor;
152155
}
153156

154157
/**
155158
* Set the {@link RecordInterceptor} to use.
156159
* @param recordInterceptor the record interceptor.
157160
*/
158-
void setRecordInterceptor(RecordInterceptor<Object, Object> recordInterceptor) {
161+
void setRecordInterceptor(@Nullable RecordInterceptor<Object, Object> recordInterceptor) {
159162
this.recordInterceptor = recordInterceptor;
160163
}
161164

162165
/**
163166
* Set the {@link BatchInterceptor} to use.
164167
* @param batchInterceptor the batch interceptor.
165168
*/
166-
void setBatchInterceptor(BatchInterceptor<Object, Object> batchInterceptor) {
169+
void setBatchInterceptor(@Nullable BatchInterceptor<Object, Object> batchInterceptor) {
167170
this.batchInterceptor = batchInterceptor;
168171
}
169172

170173
/**
171174
* Set the thread name supplier to use.
172175
* @param threadNameSupplier the thread name supplier to use
173176
*/
174-
void setThreadNameSupplier(Function<MessageListenerContainer, String> threadNameSupplier) {
177+
void setThreadNameSupplier(@Nullable Function<MessageListenerContainer, String> threadNameSupplier) {
175178
this.threadNameSupplier = threadNameSupplier;
176179
}
177180

178181
/**
179182
* Set the executor for threads that poll the consumer.
180183
* @param listenerTaskExecutor task executor
181184
*/
182-
void setListenerTaskExecutor(SimpleAsyncTaskExecutor listenerTaskExecutor) {
185+
void setListenerTaskExecutor(@Nullable SimpleAsyncTaskExecutor listenerTaskExecutor) {
183186
this.listenerTaskExecutor = listenerTaskExecutor;
184187
}
185188

@@ -199,6 +202,7 @@ public void configure(ConcurrentKafkaListenerContainerFactory<Object, Object> li
199202

200203
private void configureListenerFactory(ConcurrentKafkaListenerContainerFactory<Object, Object> factory) {
201204
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
205+
Assert.state(this.properties != null, "'properties' must not be null");
202206
Listener properties = this.properties.getListener();
203207
map.from(properties::getConcurrency).to(factory::setConcurrency);
204208
map.from(properties::isAutoStartup).to(factory::setAutoStartup);
@@ -219,6 +223,7 @@ private void configureListenerFactory(ConcurrentKafkaListenerContainerFactory<Ob
219223

220224
private void configureContainer(ContainerProperties container) {
221225
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
226+
Assert.state(this.properties != null, "'properties' must not be null");
222227
Listener properties = this.properties.getListener();
223228
map.from(properties::getAckMode).to(container::setAckMode);
224229
map.from(properties::getAsyncAcks).to(container::setAsyncAcks);

module/spring-boot-kafka/src/main/java/org/springframework/boot/kafka/autoconfigure/KafkaAnnotationDrivenConfiguration.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.function.Function;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.beans.factory.ObjectProvider;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2325
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -62,27 +64,27 @@ class KafkaAnnotationDrivenConfiguration {
6264

6365
private final KafkaProperties properties;
6466

65-
private final RecordMessageConverter recordMessageConverter;
67+
private final @Nullable RecordMessageConverter recordMessageConverter;
6668

67-
private final RecordFilterStrategy<Object, Object> recordFilterStrategy;
69+
private final @Nullable RecordFilterStrategy<Object, Object> recordFilterStrategy;
6870

6971
private final BatchMessageConverter batchMessageConverter;
7072

71-
private final KafkaTemplate<Object, Object> kafkaTemplate;
73+
private final @Nullable KafkaTemplate<Object, Object> kafkaTemplate;
7274

73-
private final KafkaAwareTransactionManager<Object, Object> transactionManager;
75+
private final @Nullable KafkaAwareTransactionManager<Object, Object> transactionManager;
7476

75-
private final ConsumerAwareRebalanceListener rebalanceListener;
77+
private final @Nullable ConsumerAwareRebalanceListener rebalanceListener;
7678

77-
private final CommonErrorHandler commonErrorHandler;
79+
private final @Nullable CommonErrorHandler commonErrorHandler;
7880

79-
private final AfterRollbackProcessor<Object, Object> afterRollbackProcessor;
81+
private final @Nullable AfterRollbackProcessor<Object, Object> afterRollbackProcessor;
8082

81-
private final RecordInterceptor<Object, Object> recordInterceptor;
83+
private final @Nullable RecordInterceptor<Object, Object> recordInterceptor;
8284

83-
private final BatchInterceptor<Object, Object> batchInterceptor;
85+
private final @Nullable BatchInterceptor<Object, Object> batchInterceptor;
8486

85-
private final Function<MessageListenerContainer, String> threadNameSupplier;
87+
private final @Nullable Function<MessageListenerContainer, String> threadNameSupplier;
8688

8789
KafkaAnnotationDrivenConfiguration(KafkaProperties properties,
8890
ObjectProvider<RecordMessageConverter> recordMessageConverter,

module/spring-boot-kafka/src/main/java/org/springframework/boot/kafka/autoconfigure/KafkaAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.kafka.clients.consumer.ConsumerConfig;
2525
import org.apache.kafka.clients.producer.ProducerConfig;
2626
import org.apache.kafka.common.config.SslConfigs;
27+
import org.jspecify.annotations.Nullable;
2728

2829
import org.springframework.aot.hint.MemberCategory;
2930
import org.springframework.aot.hint.RuntimeHints;
@@ -240,14 +241,14 @@ private static void setBackOffPolicy(RetryTopicConfigurationBuilder builder, Bac
240241
}
241242
}
242243

243-
static void applySslBundle(Map<String, Object> properties, SslBundle sslBundle) {
244+
static void applySslBundle(Map<String, Object> properties, @Nullable SslBundle sslBundle) {
244245
if (sslBundle != null) {
245246
properties.put(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG, SslBundleSslEngineFactory.class);
246247
properties.put(SslBundle.class.getName(), sslBundle);
247248
}
248249
}
249250

250-
static void applySecurityProtocol(Map<String, Object> properties, String securityProtocol) {
251+
static void applySecurityProtocol(Map<String, Object> properties, @Nullable String securityProtocol) {
251252
if (StringUtils.hasLength(securityProtocol)) {
252253
properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, securityProtocol);
253254
}
@@ -256,7 +257,7 @@ static void applySecurityProtocol(Map<String, Object> properties, String securit
256257
static class KafkaRuntimeHints implements RuntimeHintsRegistrar {
257258

258259
@Override
259-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
260+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
260261
hints.reflection().registerType(SslBundleSslEngineFactory.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
261262
}
262263

module/spring-boot-kafka/src/main/java/org/springframework/boot/kafka/autoconfigure/KafkaConnectionDetails.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
2224
import org.springframework.boot.ssl.SslBundle;
2325

@@ -41,15 +43,15 @@ public interface KafkaConnectionDetails extends ConnectionDetails {
4143
* Returns the SSL bundle.
4244
* @return the SSL bundle
4345
*/
44-
default SslBundle getSslBundle() {
46+
default @Nullable SslBundle getSslBundle() {
4547
return null;
4648
}
4749

4850
/**
4951
* Returns the security protocol.
5052
* @return the security protocol
5153
*/
52-
default String getSecurityProtocol() {
54+
default @Nullable String getSecurityProtocol() {
5355
return null;
5456
}
5557

@@ -117,20 +119,21 @@ static Configuration of(List<String> bootstrapServers, SslBundle sslBundle) {
117119
* @param securityProtocol the security protocol
118120
* @return the configuration
119121
*/
120-
static Configuration of(List<String> bootstrapServers, SslBundle sslBundle, String securityProtocol) {
122+
static Configuration of(List<String> bootstrapServers, @Nullable SslBundle sslBundle,
123+
@Nullable String securityProtocol) {
121124
return new Configuration() {
122125
@Override
123126
public List<String> getBootstrapServers() {
124127
return bootstrapServers;
125128
}
126129

127130
@Override
128-
public SslBundle getSslBundle() {
131+
public @Nullable SslBundle getSslBundle() {
129132
return sslBundle;
130133
}
131134

132135
@Override
133-
public String getSecurityProtocol() {
136+
public @Nullable String getSecurityProtocol() {
134137
return securityProtocol;
135138
}
136139
};
@@ -146,15 +149,15 @@ public String getSecurityProtocol() {
146149
* Returns the SSL bundle.
147150
* @return the SSL bundle
148151
*/
149-
default SslBundle getSslBundle() {
152+
default @Nullable SslBundle getSslBundle() {
150153
return null;
151154
}
152155

153156
/**
154157
* Returns the security protocol.
155158
* @return the security protocol
156159
*/
157-
default String getSecurityProtocol() {
160+
default @Nullable String getSecurityProtocol() {
158161
return null;
159162
}
160163

0 commit comments

Comments
 (0)