Skip to content

Commit fccc768

Browse files
committed
Add nullability annotations to module/spring-boot-cache
See gh-46587
1 parent f135a2d commit fccc768

18 files changed

+92
-48
lines changed

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/actuate/endpoint/CachesEndpoint.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Objects;
2323
import java.util.function.Predicate;
2424

25+
import org.jspecify.annotations.Nullable;
26+
2527
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
2628
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
2729
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@@ -30,6 +32,7 @@
3032
import org.springframework.boot.actuate.endpoint.annotation.Selector;
3133
import org.springframework.cache.Cache;
3234
import org.springframework.cache.CacheManager;
35+
import org.springframework.util.Assert;
3336

3437
/**
3538
* {@link Endpoint @Endpoint} to expose available {@link Cache caches}.
@@ -79,7 +82,8 @@ public CachesDescriptor caches() {
7982
* {@code cacheManager} was provided to identify a unique candidate
8083
*/
8184
@ReadOperation
82-
public CacheEntryDescriptor cache(@Selector String cache, @OptionalParameter String cacheManager) {
85+
public @Nullable CacheEntryDescriptor cache(@Selector String cache,
86+
@OptionalParameter @Nullable String cacheManager) {
8387
return extractUniqueCacheEntry(cache, getCacheEntries((name) -> name.equals(cache), isNameMatch(cacheManager)));
8488
}
8589

@@ -101,7 +105,7 @@ public void clearCaches() {
101105
* {@code cacheManager} was provided to identify a unique candidate
102106
*/
103107
@DeleteOperation
104-
public boolean clearCache(@Selector String cache, @OptionalParameter String cacheManager) {
108+
public boolean clearCache(@Selector String cache, @OptionalParameter @Nullable String cacheManager) {
105109
CacheEntryDescriptor entry = extractUniqueCacheEntry(cache,
106110
getCacheEntries((name) -> name.equals(cache), isNameMatch(cacheManager)));
107111
return (entry != null && clearCache(entry));
@@ -118,6 +122,7 @@ private List<CacheEntryDescriptor> getCacheEntries(Predicate<String> cacheNamePr
118122

119123
private List<CacheEntryDescriptor> getCacheEntries(String cacheManagerName, Predicate<String> cacheNamePredicate) {
120124
CacheManager cacheManager = this.cacheManagers.get(cacheManagerName);
125+
Assert.state(cacheManager != null, "'cacheManager' must not be null");
121126
return cacheManager.getCacheNames()
122127
.stream()
123128
.filter(cacheNamePredicate)
@@ -127,7 +132,7 @@ private List<CacheEntryDescriptor> getCacheEntries(String cacheManagerName, Pred
127132
.toList();
128133
}
129134

130-
private CacheEntryDescriptor extractUniqueCacheEntry(String cache, List<CacheEntryDescriptor> entries) {
135+
private @Nullable CacheEntryDescriptor extractUniqueCacheEntry(String cache, List<CacheEntryDescriptor> entries) {
131136
if (entries.size() > 1) {
132137
throw new NonUniqueCacheException(cache,
133138
entries.stream().map(CacheEntryDescriptor::getCacheManager).distinct().toList());
@@ -137,16 +142,18 @@ private CacheEntryDescriptor extractUniqueCacheEntry(String cache, List<CacheEnt
137142

138143
private boolean clearCache(CacheEntryDescriptor entry) {
139144
String cacheName = entry.getName();
140-
String cacheManager = entry.getCacheManager();
141-
Cache cache = this.cacheManagers.get(cacheManager).getCache(cacheName);
145+
String cacheManagerName = entry.getCacheManager();
146+
CacheManager cacheManager = this.cacheManagers.get(cacheManagerName);
147+
Assert.state(cacheManager != null, "'cacheManager' must not be null");
148+
Cache cache = cacheManager.getCache(cacheName);
142149
if (cache != null) {
143150
cache.clear();
144151
return true;
145152
}
146153
return false;
147154
}
148155

149-
private Predicate<String> isNameMatch(String name) {
156+
private Predicate<String> isNameMatch(@Nullable String name) {
150157
return (name != null) ? ((requested) -> requested.equals(name)) : matchAll();
151158
}
152159

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/actuate/endpoint/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Actuator endpoint for caches.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.cache.actuate.endpoint;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/CacheManagerCustomizers.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.boot.util.LambdaSafe;
2426
import org.springframework.cache.CacheManager;
2527

@@ -34,7 +36,7 @@ public class CacheManagerCustomizers {
3436

3537
private final List<CacheManagerCustomizer<?>> customizers;
3638

37-
public CacheManagerCustomizers(List<? extends CacheManagerCustomizer<?>> customizers) {
39+
public CacheManagerCustomizers(@Nullable List<? extends CacheManagerCustomizer<?>> customizers) {
3840
this.customizers = (customizers != null) ? new ArrayList<>(customizers) : Collections.emptyList();
3941
}
4042

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/CacheProperties.java

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

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.boot.autoconfigure.cache.CacheType;
2426
import org.springframework.boot.context.properties.ConfigurationProperties;
2527
import org.springframework.core.io.Resource;
@@ -39,7 +41,7 @@ public class CacheProperties {
3941
/**
4042
* Cache type. By default, auto-detected according to the environment.
4143
*/
42-
private CacheType type;
44+
private @Nullable CacheType type;
4345

4446
/**
4547
* List of cache names to create if supported by the underlying cache manager.
@@ -57,11 +59,11 @@ public class CacheProperties {
5759

5860
private final Redis redis = new Redis();
5961

60-
public CacheType getType() {
62+
public @Nullable CacheType getType() {
6163
return this.type;
6264
}
6365

64-
public void setType(CacheType mode) {
66+
public void setType(@Nullable CacheType mode) {
6567
this.type = mode;
6668
}
6769

@@ -100,7 +102,7 @@ public Redis getRedis() {
100102
* @throws IllegalArgumentException if the config attribute is set to an unknown
101103
* location
102104
*/
103-
public Resource resolveConfigLocation(Resource config) {
105+
public @Nullable Resource resolveConfigLocation(@Nullable Resource config) {
104106
if (config != null) {
105107
Assert.isTrue(config.exists(),
106108
() -> "'config' resource [%s] must exist".formatted(config.getDescription()));
@@ -118,13 +120,13 @@ public static class Caffeine {
118120
* The spec to use to create caches. See CaffeineSpec for more details on the spec
119121
* format.
120122
*/
121-
private String spec;
123+
private @Nullable String spec;
122124

123-
public String getSpec() {
125+
public @Nullable String getSpec() {
124126
return this.spec;
125127
}
126128

127-
public void setSpec(String spec) {
129+
public void setSpec(@Nullable String spec) {
128130
this.spec = spec;
129131
}
130132

@@ -139,13 +141,13 @@ public static class Couchbase {
139141
* Entry expiration. By default the entries never expire. Note that this value is
140142
* ultimately converted to seconds.
141143
*/
142-
private Duration expiration;
144+
private @Nullable Duration expiration;
143145

144-
public Duration getExpiration() {
146+
public @Nullable Duration getExpiration() {
145147
return this.expiration;
146148
}
147149

148-
public void setExpiration(Duration expiration) {
150+
public void setExpiration(@Nullable Duration expiration) {
149151
this.expiration = expiration;
150152
}
151153

@@ -159,13 +161,13 @@ public static class Infinispan {
159161
/**
160162
* The location of the configuration file to use to initialize Infinispan.
161163
*/
162-
private Resource config;
164+
private @Nullable Resource config;
163165

164-
public Resource getConfig() {
166+
public @Nullable Resource getConfig() {
165167
return this.config;
166168
}
167169

168-
public void setConfig(Resource config) {
170+
public void setConfig(@Nullable Resource config) {
169171
this.config = config;
170172
}
171173

@@ -180,28 +182,28 @@ public static class JCache {
180182
* The location of the configuration file to use to initialize the cache manager.
181183
* The configuration file is dependent of the underlying cache implementation.
182184
*/
183-
private Resource config;
185+
private @Nullable Resource config;
184186

185187
/**
186188
* Fully qualified name of the CachingProvider implementation to use to retrieve
187189
* the JSR-107 compliant cache manager. Needed only if more than one JSR-107
188190
* implementation is available on the classpath.
189191
*/
190-
private String provider;
192+
private @Nullable String provider;
191193

192-
public String getProvider() {
194+
public @Nullable String getProvider() {
193195
return this.provider;
194196
}
195197

196-
public void setProvider(String provider) {
198+
public void setProvider(@Nullable String provider) {
197199
this.provider = provider;
198200
}
199201

200-
public Resource getConfig() {
202+
public @Nullable Resource getConfig() {
201203
return this.config;
202204
}
203205

204-
public void setConfig(Resource config) {
206+
public void setConfig(@Nullable Resource config) {
205207
this.config = config;
206208
}
207209

@@ -215,7 +217,7 @@ public static class Redis {
215217
/**
216218
* Entry expiration. By default the entries never expire.
217219
*/
218-
private Duration timeToLive;
220+
private @Nullable Duration timeToLive;
219221

220222
/**
221223
* Allow caching null values.
@@ -225,7 +227,7 @@ public static class Redis {
225227
/**
226228
* Key prefix.
227229
*/
228-
private String keyPrefix;
230+
private @Nullable String keyPrefix;
229231

230232
/**
231233
* Whether to use the key prefix when writing to Redis.
@@ -237,11 +239,11 @@ public static class Redis {
237239
*/
238240
private boolean enableStatistics;
239241

240-
public Duration getTimeToLive() {
242+
public @Nullable Duration getTimeToLive() {
241243
return this.timeToLive;
242244
}
243245

244-
public void setTimeToLive(Duration timeToLive) {
246+
public void setTimeToLive(@Nullable Duration timeToLive) {
245247
this.timeToLive = timeToLive;
246248
}
247249

@@ -253,11 +255,11 @@ public void setCacheNullValues(boolean cacheNullValues) {
253255
this.cacheNullValues = cacheNullValues;
254256
}
255257

256-
public String getKeyPrefix() {
258+
public @Nullable String getKeyPrefix() {
257259
return this.keyPrefix;
258260
}
259261

260-
public void setKeyPrefix(String keyPrefix) {
262+
public void setKeyPrefix(@Nullable String keyPrefix) {
261263
this.keyPrefix = keyPrefix;
262264
}
263265

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/CaffeineCacheConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.github.benmanes.caffeine.cache.CacheLoader;
2222
import com.github.benmanes.caffeine.cache.Caffeine;
2323
import com.github.benmanes.caffeine.cache.CaffeineSpec;
24+
import org.jspecify.annotations.Nullable;
2425

2526
import org.springframework.beans.factory.ObjectProvider;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -65,8 +66,8 @@ private CaffeineCacheManager createCacheManager(CacheProperties cacheProperties,
6566
return cacheManager;
6667
}
6768

68-
private void setCacheBuilder(CacheProperties cacheProperties, CaffeineSpec caffeineSpec,
69-
Caffeine<Object, Object> caffeine, CaffeineCacheManager cacheManager) {
69+
private void setCacheBuilder(CacheProperties cacheProperties, @Nullable CaffeineSpec caffeineSpec,
70+
@Nullable Caffeine<Object, Object> caffeine, CaffeineCacheManager cacheManager) {
7071
String specification = cacheProperties.getCaffeine().getSpec();
7172
if (StringUtils.hasText(specification)) {
7273
cacheManager.setCacheSpecification(specification);

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/HazelcastJCacheCustomizationConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Properties;
2222

2323
import com.hazelcast.core.HazelcastInstance;
24+
import org.jspecify.annotations.Nullable;
2425

2526
import org.springframework.beans.factory.ObjectProvider;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -45,11 +46,11 @@ HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<Hazel
4546

4647
static class HazelcastPropertiesCustomizer implements JCachePropertiesCustomizer {
4748

48-
private final HazelcastInstance hazelcastInstance;
49+
private final @Nullable HazelcastInstance hazelcastInstance;
4950

5051
private final CacheProperties cacheProperties;
5152

52-
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance, CacheProperties cacheProperties) {
53+
HazelcastPropertiesCustomizer(@Nullable HazelcastInstance hazelcastInstance, CacheProperties cacheProperties) {
5354
this.hazelcastInstance = hazelcastInstance;
5455
this.cacheProperties = cacheProperties;
5556
}

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/InfinispanCacheConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.infinispan.manager.DefaultCacheManager;
2525
import org.infinispan.manager.EmbeddedCacheManager;
2626
import org.infinispan.spring.embedded.provider.SpringEmbeddedCacheManager;
27+
import org.jspecify.annotations.Nullable;
2728

2829
import org.springframework.beans.factory.ObjectProvider;
2930
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -80,7 +81,7 @@ private EmbeddedCacheManager createEmbeddedCacheManager(CacheProperties cachePro
8081
}
8182

8283
private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration(
83-
ConfigurationBuilder defaultConfigurationBuilder) {
84+
@Nullable ConfigurationBuilder defaultConfigurationBuilder) {
8485
if (defaultConfigurationBuilder != null) {
8586
return defaultConfigurationBuilder.build();
8687
}

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/JCacheCacheConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import javax.cache.configuration.MutableConfiguration;
2727
import javax.cache.spi.CachingProvider;
2828

29+
import org.jspecify.annotations.Nullable;
30+
2931
import org.springframework.beans.factory.BeanClassLoaderAware;
3032
import org.springframework.beans.factory.ObjectProvider;
3133
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
@@ -61,6 +63,7 @@
6163
@Import(HazelcastJCacheCustomizationConfiguration.class)
6264
class JCacheCacheConfiguration implements BeanClassLoaderAware {
6365

66+
@SuppressWarnings("NullAway.Init")
6467
private ClassLoader beanClassLoader;
6568

6669
@Override
@@ -103,7 +106,7 @@ private CacheManager createCacheManager(CacheProperties cacheProperties,
103106
return cachingProvider.getCacheManager(null, this.beanClassLoader, properties);
104107
}
105108

106-
private CachingProvider getCachingProvider(String cachingProviderFqn) {
109+
private CachingProvider getCachingProvider(@Nullable String cachingProviderFqn) {
107110
if (StringUtils.hasText(cachingProviderFqn)) {
108111
return Caching.getCachingProvider(cachingProviderFqn);
109112
}

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/RedisCacheConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.LinkedHashSet;
2020
import java.util.List;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.beans.factory.ObjectProvider;
2325
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2426
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -73,12 +75,12 @@ RedisCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCust
7375
private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
7476
CacheProperties cacheProperties,
7577
ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
76-
ClassLoader classLoader) {
78+
@Nullable ClassLoader classLoader) {
7779
return redisCacheConfiguration.getIfAvailable(() -> createConfiguration(cacheProperties, classLoader));
7880
}
7981

8082
private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration(
81-
CacheProperties cacheProperties, ClassLoader classLoader) {
83+
CacheProperties cacheProperties, @Nullable ClassLoader classLoader) {
8284
Redis redisProperties = cacheProperties.getRedis();
8385
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
8486
.defaultCacheConfig();

module/spring-boot-cache/src/main/java/org/springframework/boot/cache/autoconfigure/endpoint/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for the cache abstraction endpoints.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.cache.autoconfigure.endpoint;
22+
23+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)