Skip to content

Commit f39f892

Browse files
committed
Add JSpecify nullability annotations to kafka.event and security.jaas packages
Add nullability annotations and make relevant fields final in spring-kafka event classes and security.jaas classes for improved type safety. Signed-off-by: Soby Chacko <[email protected]>
1 parent aa78bd4 commit f39f892

12 files changed

+42
-33
lines changed

spring-kafka/src/main/java/org/springframework/kafka/event/ConsumerPausedEvent.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2023 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collection;
2020

2121
import org.apache.kafka.common.TopicPartition;
22+
import org.jspecify.annotations.Nullable;
2223

2324
/**
2425
* An event published when a consumer is paused.
@@ -31,7 +32,7 @@ public class ConsumerPausedEvent extends KafkaEvent {
3132

3233
private static final long serialVersionUID = 1L;
3334

34-
private final String reason;
35+
private final @Nullable String reason;
3536

3637
private transient Collection<TopicPartition> partitions;
3738

spring-kafka/src/main/java/org/springframework/kafka/event/ConsumerStoppingEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2023 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,9 +35,9 @@ public class ConsumerStoppingEvent extends KafkaEvent {
3535

3636
private static final long serialVersionUID = 1L;
3737

38-
private transient Consumer<?, ?> consumer;
38+
private transient final Consumer<?, ?> consumer;
3939

40-
private transient Collection<TopicPartition> partitions;
40+
private transient final Collection<TopicPartition> partitions;
4141

4242
/**
4343
* Construct an instance with the provided source, consumer and partitions.

spring-kafka/src/main/java/org/springframework/kafka/event/KafkaEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2024 the original author or authors.
2+
* Copyright 2015-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ public abstract class KafkaEvent extends ApplicationEvent {
2929

3030
private static final long serialVersionUID = 1L;
3131

32-
private transient Object container;
32+
private transient final Object container;
3333

3434
public KafkaEvent(Object source, Object container) {
3535
super(source);

spring-kafka/src/main/java/org/springframework/kafka/event/ListenerContainerIdleEvent.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 the original author or authors.
2+
* Copyright 2016-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.apache.kafka.clients.consumer.Consumer;
2525
import org.apache.kafka.common.TopicPartition;
26+
import org.jspecify.annotations.Nullable;
2627

2728
/**
2829
* An event that is emitted when a container is idle if the container
@@ -41,9 +42,9 @@ public class ListenerContainerIdleEvent extends KafkaEvent {
4142

4243
private final boolean paused;
4344

44-
private transient List<TopicPartition> topicPartitions;
45+
private transient final @Nullable List<TopicPartition> topicPartitions;
4546

46-
private transient Consumer<?, ?> consumer;
47+
private transient final Consumer<?, ?> consumer;
4748

4849
/**
4950
* Construct an instance with the provided arguments.
@@ -58,7 +59,7 @@ public class ListenerContainerIdleEvent extends KafkaEvent {
5859
*/
5960
public ListenerContainerIdleEvent(Object source, Object container,
6061
long idleTime, String id,
61-
Collection<TopicPartition> topicPartitions, Consumer<?, ?> consumer, boolean paused) {
62+
@Nullable Collection<TopicPartition> topicPartitions, Consumer<?, ?> consumer, boolean paused) {
6263

6364
super(source, container);
6465
this.idleTime = idleTime;
@@ -80,7 +81,7 @@ public long getIdleTime() {
8081
* The TopicPartitions the container is listening to.
8182
* @return the TopicPartition list.
8283
*/
83-
public Collection<TopicPartition> getTopicPartitions() {
84+
public @Nullable Collection<TopicPartition> getTopicPartitions() {
8485
return this.topicPartitions == null ? null : Collections.unmodifiableList(this.topicPartitions);
8586
}
8687

spring-kafka/src/main/java/org/springframework/kafka/event/ListenerContainerNoLongerIdleEvent.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.apache.kafka.clients.consumer.Consumer;
2525
import org.apache.kafka.common.TopicPartition;
26+
import org.jspecify.annotations.Nullable;
2627

2728
/**
2829
* An event that is emitted when a container is no longer idle if configured to publish
@@ -39,9 +40,9 @@ public class ListenerContainerNoLongerIdleEvent extends KafkaEvent {
3940

4041
private final String listenerId;
4142

42-
private transient List<TopicPartition> topicPartitions;
43+
private transient final @Nullable List<TopicPartition> topicPartitions;
4344

44-
private transient Consumer<?, ?> consumer;
45+
private transient final @Nullable Consumer<?, ?> consumer;
4546

4647
/**
4748
* Construct an instance with the provided arguments.
@@ -53,7 +54,7 @@ public class ListenerContainerNoLongerIdleEvent extends KafkaEvent {
5354
* @param consumer the consumer.
5455
*/
5556
public ListenerContainerNoLongerIdleEvent(Object source, Object container, long idleTime, String id,
56-
Collection<TopicPartition> topicPartitions, Consumer<?, ?> consumer) {
57+
@Nullable Collection<TopicPartition> topicPartitions, Consumer<?, ?> consumer) {
5758

5859
super(source, container);
5960
this.idleTime = idleTime;
@@ -66,7 +67,7 @@ public ListenerContainerNoLongerIdleEvent(Object source, Object container, long
6667
* The TopicPartitions the container is listening to.
6768
* @return the TopicPartition list.
6869
*/
69-
public Collection<TopicPartition> getTopicPartitions() {
70+
public @Nullable Collection<TopicPartition> getTopicPartitions() {
7071
return this.topicPartitions == null ? null : Collections.unmodifiableList(this.topicPartitions);
7172
}
7273

@@ -91,7 +92,7 @@ public String getListenerId() {
9192
* Allows the listener to resume a paused consumer.
9293
* @return the consumer.
9394
*/
94-
public Consumer<?, ?> getConsumer() {
95+
public @Nullable Consumer<?, ?> getConsumer() {
9596
return this.consumer;
9697
}
9798

spring-kafka/src/main/java/org/springframework/kafka/event/ListenerContainerPartitionIdleEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 the original author or authors.
2+
* Copyright 2016-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ public class ListenerContainerPartitionIdleEvent extends KafkaEvent {
3939

4040
private final boolean paused;
4141

42-
private transient Consumer<?, ?> consumer;
42+
private transient final Consumer<?, ?> consumer;
4343

4444
/**
4545
* Construct an instance with the provided arguments.

spring-kafka/src/main/java/org/springframework/kafka/event/ListenerContainerPartitionNoLongerIdleEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ public class ListenerContainerPartitionNoLongerIdleEvent extends KafkaEvent {
3737

3838
private final TopicPartition topicPartition;
3939

40-
private transient Consumer<?, ?> consumer;
40+
private transient final Consumer<?, ?> consumer;
4141

4242
/**
4343
* Construct an instance with the provided arguments.

spring-kafka/src/main/java/org/springframework/kafka/event/NonResponsiveConsumerEvent.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.apache.kafka.clients.consumer.Consumer;
2525
import org.apache.kafka.common.TopicPartition;
26+
import org.jspecify.annotations.Nullable;
2627

2728
/**
2829
* An event that is emitted when a consumer is not responding to the poll; with early
@@ -40,9 +41,9 @@ public class NonResponsiveConsumerEvent extends KafkaEvent {
4041

4142
private final String listenerId;
4243

43-
private final List<TopicPartition> topicPartitions;
44+
private final @Nullable List<TopicPartition> topicPartitions;
4445

45-
private transient Consumer<?, ?> consumer;
46+
private transient final Consumer<?, ?> consumer;
4647

4748
/**
4849
* Construct an instance with the provided properties.
@@ -56,7 +57,7 @@ public class NonResponsiveConsumerEvent extends KafkaEvent {
5657
*/
5758
public NonResponsiveConsumerEvent(Object source, Object container,
5859
long timeSinceLastPoll, String id,
59-
Collection<TopicPartition> topicPartitions, Consumer<?, ?> consumer) {
60+
@Nullable Collection<TopicPartition> topicPartitions, Consumer<?, ?> consumer) {
6061

6162
super(source, container);
6263
this.timeSinceLastPoll = timeSinceLastPoll;
@@ -77,7 +78,7 @@ public long getTimeSinceLastPoll() {
7778
* The TopicPartitions the container is listening to.
7879
* @return the TopicPartition list.
7980
*/
80-
public Collection<TopicPartition> getTopicPartitions() {
81+
public @Nullable Collection<TopicPartition> getTopicPartitions() {
8182
return this.topicPartitions == null ? null : Collections.unmodifiableList(this.topicPartitions);
8283
}
8384

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Application Events.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.kafka.event;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Base package for kafka
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.kafka;

spring-kafka/src/main/java/org/springframework/kafka/security/jaas/KafkaJaasLoginModuleInitializer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2024 the original author or authors.
2+
* Copyright 2017-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import javax.security.auth.login.Configuration;
2525

2626
import org.apache.kafka.common.security.JaasUtils;
27+
import org.jspecify.annotations.Nullable;
2728

2829
import org.springframework.beans.factory.DisposableBean;
2930
import org.springframework.beans.factory.SmartInitializingSingleton;
@@ -124,7 +125,7 @@ public void setOptions(Map<String, String> options) {
124125
public void afterSingletonsInstantiated() {
125126
// only use programmatic support if a file is not set via system property
126127
if (this.ignoreJavaLoginConfigParamSystemProperty) {
127-
Map<String, AppConfigurationEntry[]> configurationEntries = new HashMap<>();
128+
Map<String, @Nullable AppConfigurationEntry[]> configurationEntries = new HashMap<>();
128129
AppConfigurationEntry kafkaClientConfigurationEntry = new AppConfigurationEntry(
129130
this.loginModule,
130131
this.controlFlag,
@@ -145,20 +146,21 @@ public void destroy() {
145146

146147
private static class InternalConfiguration extends Configuration {
147148

148-
private final Map<String, AppConfigurationEntry[]> configurationEntries;
149+
private final Map<String, @Nullable AppConfigurationEntry[]> configurationEntries;
149150

150151
private final Configuration delegate;
151152

152-
InternalConfiguration(Map<String, AppConfigurationEntry[]> configurationEntries, Configuration delegate) {
153+
InternalConfiguration(Map<String, @Nullable AppConfigurationEntry[]> configurationEntries, Configuration delegate) {
153154
Assert.notNull(configurationEntries, "'configurationEntries' cannot be null");
154155
Assert.notEmpty(configurationEntries, "'configurationEntries' cannot be empty");
155156
this.configurationEntries = configurationEntries;
156157
this.delegate = delegate;
157158
}
158159

159160
@Override
160-
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
161-
AppConfigurationEntry[] conf = this.delegate == null ? null : this.delegate.getAppConfigurationEntry(name);
161+
@SuppressWarnings("NullAway") // Overridden method does not define nullness
162+
public @Nullable AppConfigurationEntry[] getAppConfigurationEntry(String name) {
163+
AppConfigurationEntry[] conf = this.delegate.getAppConfigurationEntry(name);
162164
return conf != null ? conf : this.configurationEntries.get(name);
163165
}
164166

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Provides classes related to jaas.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.kafka.security.jaas;

0 commit comments

Comments
 (0)