Skip to content

Commit f368147

Browse files
committed
Refine null-safety in the spring-context module
Closes gh-34151
1 parent cf90bee commit f368147

19 files changed

+26
-31
lines changed

Diff for: spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -107,7 +107,7 @@ public CacheResolver getCacheResolver() {
107107
}
108108

109109
@Override
110-
@SuppressWarnings("NullAway")
110+
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1113
111111
public CacheInvocationParameter[] getAllParameters(@Nullable Object... values) {
112112
if (this.allParameterDetails.size() != values.length) {
113113
throw new IllegalStateException("Values mismatch, operation has " +

Diff for: spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -182,7 +182,7 @@ protected <T> T getBean(Class<T> type) {
182182
}
183183
}
184184

185-
@SuppressWarnings("NullAway")
185+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
186186
protected CacheManager getDefaultCacheManager() {
187187
if (getCacheManager() == null) {
188188
Assert.state(this.beanFactory != null, "BeanFactory required for default CacheManager resolution");
@@ -202,7 +202,7 @@ protected CacheManager getDefaultCacheManager() {
202202
}
203203

204204
@Override
205-
@SuppressWarnings("NullAway")
205+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
206206
protected CacheResolver getDefaultCacheResolver() {
207207
if (getCacheResolver() == null) {
208208
this.cacheResolver = SingletonSupplier.of(new SimpleCacheResolver(getDefaultCacheManager()));
@@ -211,7 +211,7 @@ protected CacheResolver getDefaultCacheResolver() {
211211
}
212212

213213
@Override
214-
@SuppressWarnings("NullAway")
214+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
215215
protected CacheResolver getDefaultExceptionCacheResolver() {
216216
if (getExceptionCacheResolver() == null) {
217217
this.exceptionCacheResolver = SingletonSupplier.of(new LazyCacheResolver());

Diff for: spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -84,7 +84,7 @@ public Object getTarget() {
8484
}
8585

8686
@Override
87-
@SuppressWarnings("NullAway")
87+
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1113
8888
public Object generate(Object target, Method method, @Nullable Object... params) {
8989
JCacheOperation<?> operation = this.cacheOperationSource.getCacheOperation(method, target.getClass());
9090
if (!(operation instanceof AbstractJCacheKeyOperation)) {

Diff for: spring-context-support/src/main/java/org/springframework/scheduling/quartz/LocalDataSourceJobStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class LocalDataSourceJobStore extends JobStoreCMT {
9090

9191

9292
@Override
93-
@SuppressWarnings("NullAway")
93+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
9494
public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) throws SchedulerConfigException {
9595
// Absolutely needs thread-bound DataSource to initialize.
9696
this.dataSource = SchedulerFactoryBean.getConfigTimeDataSource();

Diff for: spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -194,7 +194,7 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
194194
/**
195195
* Register jobs and triggers (within a transaction, if possible).
196196
*/
197-
@SuppressWarnings("NullAway")
197+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
198198
protected void registerJobsAndTriggers() throws SchedulerException {
199199
TransactionStatus transactionStatus = null;
200200
if (this.transactionManager != null) {

Diff for: spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ private Scheduler prepareScheduler(SchedulerFactory schedulerFactory) throws Sch
644644
* @see #afterPropertiesSet
645645
* @see org.quartz.SchedulerFactory#getScheduler
646646
*/
647-
@SuppressWarnings("NullAway")
647+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
648648
protected Scheduler createScheduler(SchedulerFactory schedulerFactory, @Nullable String schedulerName)
649649
throws SchedulerException {
650650

Diff for: spring-context/src/main/java/org/springframework/cache/interceptor/SimpleKeyGenerator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -43,7 +43,7 @@
4343
public class SimpleKeyGenerator implements KeyGenerator {
4444

4545
@Override
46-
@SuppressWarnings("NullAway")
46+
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1113
4747
public Object generate(Object target, Method method, @Nullable Object... params) {
4848
return generateKey((KotlinDetector.isSuspendingFunction(method) ?
4949
Arrays.copyOf(params, params.length - 1) : params));

Diff for: spring-context/src/main/java/org/springframework/context/annotation/BeanMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class BeanMethod extends ConfigurationMethod {
4545

4646

4747
@Override
48-
@SuppressWarnings("NullAway")
48+
@SuppressWarnings("NullAway") // Reflection
4949
public void validate(ProblemReporter problemReporter) {
5050
if (getMetadata().getAnnotationAttributes(Autowired.class.getName()) != null) {
5151
// declared as @Autowired: semantic mismatch since @Bean method arguments are autowired

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Map<ImportBeanDefinitionRegistrar, AnnotationMetadata> getImportBeanDefinitionRe
218218
return this.importBeanDefinitionRegistrars;
219219
}
220220

221-
@SuppressWarnings("NullAway")
221+
@SuppressWarnings("NullAway") // Reflection
222222
void validate(ProblemReporter problemReporter) {
223223
Map<String, Object> attributes = this.metadata.getAnnotationAttributes(Configuration.class.getName());
224224

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
291291
this.registry.registerBeanDefinition(beanName, beanDefToRegister);
292292
}
293293

294-
@SuppressWarnings("NullAway")
294+
@SuppressWarnings("NullAway") // Reflection
295295
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) {
296296
if (!this.registry.containsBeanDefinition(beanName)) {
297297
return false;

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -821,12 +821,12 @@ void register(DeferredImportSelectorHolder deferredImport) {
821821
deferredImport.getConfigurationClass());
822822
}
823823

824-
@SuppressWarnings("NullAway")
825824
void processGroupImports() {
826825
for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
827826
Predicate<String> filter = grouping.getCandidateFilter();
828827
grouping.getImports().forEach(entry -> {
829828
ConfigurationClass configurationClass = this.configurationClasses.get(entry.getMetadata());
829+
Assert.state(configurationClass != null, "ConfigurationClass must not be null");
830830
try {
831831
processImports(configurationClass, asSourceClass(configurationClass, filter),
832832
Collections.singleton(asSourceClass(entry.getImportClassName(), filter)),

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
319319
}
320320

321321
@Override
322-
@SuppressWarnings("NullAway")
322+
@SuppressWarnings("NullAway") // Lazy initialization
323323
public @Nullable BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
324324
boolean hasPropertySourceDescriptors = !CollectionUtils.isEmpty(this.propertySourceDescriptors);
325325
boolean hasImportRegistry = beanFactory.containsBean(IMPORT_REGISTRY_BEAN_NAME);

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public Class<?> getTargetClass() {
127127
}
128128

129129
@Override
130-
@SuppressWarnings("NullAway")
131130
public Object getTarget() {
132131
Object cachedTarget = this.cachedTarget;
133132
if (cachedTarget != null) {

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ProfileCondition.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -31,7 +31,7 @@
3131
class ProfileCondition implements Condition {
3232

3333
@Override
34-
@SuppressWarnings("NullAway")
34+
@SuppressWarnings("NullAway") // Reflection
3535
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
3636
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
3737
if (attrs != null) {

Diff for: spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -229,7 +229,7 @@ protected Collection<ApplicationListener<?>> getApplicationListeners(
229229
* @param retriever the ListenerRetriever, if supposed to populate one (for caching purposes)
230230
* @return the pre-filtered list of application listeners for the given event and source type
231231
*/
232-
@SuppressWarnings("NullAway")
232+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
233233
private Collection<ApplicationListener<?>> retrieveApplicationListeners(
234234
ResolvableType eventType, @Nullable Class<?> sourceType, @Nullable CachedListenerRetriever retriever) {
235235

Diff for: spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* @see org.springframework.format.FormatterRegistrar#registerFormatters
5252
* @see org.springframework.format.datetime.DateFormatterRegistrar
5353
*/
54-
@SuppressWarnings("NullAway")
54+
@SuppressWarnings("NullAway") // Well-known map keys
5555
public class DateTimeFormatterRegistrar implements FormatterRegistrar {
5656

5757
private enum Type {DATE, TIME, DATE_TIME}

Diff for: spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java

-1
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,6 @@ private void registerNotificationListeners() throws MBeanExportException {
987987
* Unregister the configured {@link NotificationListener NotificationListeners}
988988
* from the {@link MBeanServer}.
989989
*/
990-
@SuppressWarnings("NullAway")
991990
private void unregisterNotificationListeners() {
992991
if (this.server != null) {
993992
this.registeredNotificationListeners.forEach((bean, mappedObjectNames) -> {

Diff for: spring-context/src/main/java/org/springframework/validation/DataBinder.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,6 @@ public void setValidator(@Nullable Validator validator) {
669669
}
670670
}
671671

672-
@SuppressWarnings("NullAway")
673672
private void assertValidators(@Nullable Validator... validators) {
674673
Object target = getTarget();
675674
for (Validator validator : validators) {
@@ -728,7 +727,6 @@ public List<Validator> getValidators() {
728727
* {@link #setExcludedValidators(Predicate) exclude predicate}.
729728
* @since 6.1
730729
*/
731-
@SuppressWarnings("NullAway")
732730
public List<Validator> getValidatorsToApply() {
733731
return (this.excludedValidators != null ?
734732
this.validators.stream().filter(validator -> !this.excludedValidators.test(validator)).toList() :
@@ -1232,7 +1230,7 @@ protected boolean isAllowed(String field) {
12321230
* @see #getBindingErrorProcessor
12331231
* @see BindingErrorProcessor#processMissingFieldError
12341232
*/
1235-
@SuppressWarnings("NullAway")
1233+
@SuppressWarnings("NullAway") // Dataflow analysis limitation
12361234
protected void checkRequiredFields(MutablePropertyValues mpvs) {
12371235
String[] requiredFields = getRequiredFields();
12381236
if (!ObjectUtils.isEmpty(requiredFields)) {

Diff for: spring-context/src/main/java/org/springframework/validation/DefaultMessageCodesResolver.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -243,7 +243,6 @@ public String format(String errorCode, @Nullable String objectName, @Nullable St
243243
* {@link DefaultMessageCodesResolver#CODE_SEPARATOR}, skipping zero-length or
244244
* null elements altogether.
245245
*/
246-
@SuppressWarnings("NullAway")
247246
public static String toDelimitedString(@Nullable String... elements) {
248247
StringJoiner rtn = new StringJoiner(CODE_SEPARATOR);
249248
for (String element : elements) {

0 commit comments

Comments
 (0)