Skip to content

Commit 290a41d

Browse files
committed
Refine null-safety in more modules
This commit refines the null-safety in all remaining modules except spring-test. See spring-projectsgh-32475
1 parent 1b563f8 commit 290a41d

File tree

88 files changed

+172
-72
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+172
-72
lines changed

spring-aop/src/main/java/org/springframework/aop/target/LazyInitTargetSource.java

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class LazyInitTargetSource extends AbstractBeanFactoryBasedTargetSource {
6565

6666

6767
@Override
68+
@Nullable
6869
public synchronized Object getTarget() throws BeansException {
6970
if (this.target == null) {
7071
this.target = getBeanFactory().getBean(getTargetBeanName());

spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ protected Collection<Cache> loadCaches() {
121121
}
122122

123123
@Override
124+
@Nullable
124125
protected Cache getMissingCache(String name) {
125126
CacheManager cacheManager = getCacheManager();
126127
Assert.state(cacheManager != null, "No CacheManager set");

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AnnotationJCacheOperationSource.java

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
public abstract class AnnotationJCacheOperationSource extends AbstractFallbackJCacheOperationSource {
4747

4848
@Override
49+
@Nullable
4950
protected JCacheOperation<?> findCacheOperation(Method method, @Nullable Class<?> targetType) {
5051
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
5152
CachePut cachePut = method.getAnnotation(CachePut.class);

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.cache.interceptor.CacheErrorHandler;
2424
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
2525
import org.springframework.cache.interceptor.CacheOperationInvoker;
26+
import org.springframework.lang.Nullable;
2627

2728
/**
2829
* Intercept methods annotated with {@link CachePut}.
@@ -39,6 +40,7 @@ public CachePutInterceptor(CacheErrorHandler errorHandler) {
3940

4041

4142
@Override
43+
@Nullable
4244
protected Object invoke(
4345
CacheOperationInvocationContext<CachePutOperation> context, CacheOperationInvoker invoker) {
4446

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveAllInterceptor.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.cache.interceptor.CacheErrorHandler;
2323
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
2424
import org.springframework.cache.interceptor.CacheOperationInvoker;
25+
import org.springframework.lang.Nullable;
2526

2627
/**
2728
* Intercept methods annotated with {@link CacheRemoveAll}.
@@ -38,6 +39,7 @@ protected CacheRemoveAllInterceptor(CacheErrorHandler errorHandler) {
3839

3940

4041
@Override
42+
@Nullable
4143
protected Object invoke(
4244
CacheOperationInvocationContext<CacheRemoveAllOperation> context, CacheOperationInvoker invoker) {
4345

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheRemoveEntryInterceptor.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.cache.interceptor.CacheErrorHandler;
2323
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
2424
import org.springframework.cache.interceptor.CacheOperationInvoker;
25+
import org.springframework.lang.Nullable;
2526

2627
/**
2728
* Intercept methods annotated with {@link CacheRemove}.
@@ -38,6 +39,7 @@ protected CacheRemoveEntryInterceptor(CacheErrorHandler errorHandler) {
3839

3940

4041
@Override
42+
@Nullable
4143
protected Object invoke(
4244
CacheOperationInvocationContext<CacheRemoveOperation> context, CacheOperationInvoker invoker) {
4345

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheAspectSupport.java

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public CacheOperationInvokerAdapter(CacheOperationInvoker delegate) {
180180
}
181181

182182
@Override
183+
@Nullable
183184
public Object invoke() throws ThrowableWrapper {
184185
return invokeOperation(this.delegate);
185186
}

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/SimpleExceptionCacheResolver.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.cache.interceptor.BasicOperation;
2525
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
2626
import org.springframework.cache.interceptor.CacheResolver;
27+
import org.springframework.lang.Nullable;
2728

2829
/**
2930
* A simple {@link CacheResolver} that resolves the exception cache
@@ -41,6 +42,7 @@ public SimpleExceptionCacheResolver(CacheManager cacheManager) {
4142
}
4243

4344
@Override
45+
@Nullable
4446
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
4547
BasicOperation operation = context.getOperation();
4648
if (!(operation instanceof CacheResultOperation cacheResultOperation)) {

spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public ValueWrapper get(Object key) {
8383
}
8484

8585
@Override
86+
@Nullable
8687
public <T> T get(Object key, @Nullable Class<T> type) {
8788
return this.targetCache.get(key, type);
8889
}

spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java

+2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ protected void postProcessJobDetail(JobDetail jobDetail) {
199199
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
200200
*/
201201
@Override
202+
@Nullable
202203
public Class<?> getTargetClass() {
203204
Class<?> targetClass = super.getTargetClass();
204205
if (targetClass == null && this.targetBeanName != null) {
@@ -212,6 +213,7 @@ public Class<?> getTargetClass() {
212213
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
213214
*/
214215
@Override
216+
@Nullable
215217
public Object getTargetObject() {
216218
Object targetObject = super.getTargetObject();
217219
if (targetObject == null && this.targetBeanName != null) {

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.aot.hint.TypeHint.Builder;
2323
import org.springframework.aot.hint.TypeReference;
2424
import org.springframework.aot.hint.annotation.ReflectiveRuntimeHintsRegistrar;
25+
import org.springframework.lang.Nullable;
2526
import org.springframework.util.ClassUtils;
2627

2728
/**
@@ -40,7 +41,7 @@ class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {
4041

4142

4243
@Override
43-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
44+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
4445
if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
4546
return;
4647
}

spring-context/src/main/java/org/springframework/cache/Cache.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class ValueRetrievalException extends RuntimeException {
315315
@Nullable
316316
private final Object key;
317317

318-
public ValueRetrievalException(@Nullable Object key, Callable<?> loader, Throwable ex) {
318+
public ValueRetrievalException(@Nullable Object key, Callable<?> loader, @Nullable Throwable ex) {
319319
super(String.format("Value for key '%s' could not be loaded using '%s'", key, loader), ex);
320320
this.key = key;
321321
}

spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ protected Object toStoreValue(@Nullable Object userValue) {
223223
}
224224

225225
@Override
226+
@Nullable
226227
protected Object fromStoreValue(@Nullable Object storeValue) {
227228
if (storeValue != null && this.serialization != null) {
228229
try {

spring-context/src/main/java/org/springframework/cache/interceptor/NamedCacheResolver.java

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void setCacheNames(Collection<String> cacheNames) {
5252
}
5353

5454
@Override
55+
@Nullable
5556
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
5657
return this.cacheNames;
5758
}

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

+2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, C
314314
}
315315

316316
@Override
317+
@Nullable
317318
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
318319
BeanRegistrationAotContribution parentAotContribution = super.processAheadOfTime(registeredBean);
319320
Class<?> beanClass = registeredBean.getBeanClass();
@@ -350,6 +351,7 @@ public void resetBeanDefinition(String beanName) {
350351
}
351352

352353
@Override
354+
@Nullable
353355
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
354356
return null;
355357
}

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
334334
return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);
335335
}
336336

337+
@Nullable
337338
private Object resolveBeanReference(Method beanMethod, Object[] beanMethodArgs,
338339
ConfigurableBeanFactory beanFactory, String beanName) {
339340

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ protected void handleAsyncError(Throwable t) {
350350
* Invoke the event listener method with the given argument values.
351351
*/
352352
@Nullable
353-
protected Object doInvoke(Object... args) {
353+
protected Object doInvoke(@Nullable Object... args) {
354354
Object bean = getTargetBean();
355355
// Detect package-protected NullBean instance through equals(null) check
356356
if (bean.equals(null)) {
@@ -416,8 +416,8 @@ protected String getCondition() {
416416
* the given error message.
417417
* @param message error message to append the HandlerMethod details to
418418
*/
419-
protected String getDetailedErrorMessage(Object bean, String message) {
420-
StringBuilder sb = new StringBuilder(message).append('\n');
419+
protected String getDetailedErrorMessage(Object bean, @Nullable String message) {
420+
StringBuilder sb = (StringUtils.hasLength(message) ? new StringBuilder(message).append('\n') : new StringBuilder());
421421
sb.append("HandlerMethod details: \n");
422422
sb.append("Bean [").append(bean.getClass().getName()).append("]\n");
423423
sb.append("Method [").append(this.method.toGenericString()).append("]\n");
@@ -431,7 +431,7 @@ protected String getDetailedErrorMessage(Object bean, String message) {
431431
* beans, and others). Event listener beans that require proxying should prefer
432432
* class-based proxy mechanisms.
433433
*/
434-
private void assertTargetBean(Method method, Object targetBean, Object[] args) {
434+
private void assertTargetBean(Method method, Object targetBean, @Nullable Object[] args) {
435435
Class<?> methodDeclaringClass = method.getDeclaringClass();
436436
Class<?> targetBeanClass = targetBean.getClass();
437437
if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
@@ -443,7 +443,7 @@ private void assertTargetBean(Method method, Object targetBean, Object[] args) {
443443
}
444444
}
445445

446-
private String getInvocationErrorMessage(Object bean, String message, Object[] resolvedArgs) {
446+
private String getInvocationErrorMessage(Object bean, @Nullable String message, @Nullable Object[] resolvedArgs) {
447447
StringBuilder sb = new StringBuilder(getDetailedErrorMessage(bean, message));
448448
sb.append("Resolved arguments: \n");
449449
for (int i = 0; i < resolvedArgs.length; i++) {

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

+1
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,7 @@ protected BeanFactory getInternalParentBeanFactory() {
14631463
//---------------------------------------------------------------------
14641464

14651465
@Override
1466+
@Nullable
14661467
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
14671468
return getMessageSource().getMessage(code, args, defaultMessage, locale);
14681469
}

spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ protected boolean isUseCodeAsDefaultMessage() {
137137

138138

139139
@Override
140+
@Nullable
140141
public final String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
141142
String msg = getMessageInternal(code, args, locale);
142143
if (msg != null) {

spring-context/src/main/java/org/springframework/context/support/ContextTypeMatchClassLoader.java

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ protected boolean isEligibleForOverriding(String className) {
123123
}
124124

125125
@Override
126+
@Nullable
126127
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
127128
byte[] bytes = bytesCache.get(name);
128129
if (bytes == null) {

spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {
191191
* returning the value found in the bundle as-is (without MessageFormat parsing).
192192
*/
193193
@Override
194+
@Nullable
194195
protected String resolveCodeWithoutArguments(String code, Locale locale) {
195196
if (getCacheMillis() < 0) {
196197
PropertiesHolder propHolder = getMergedProperties(locale);

spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public void setBeanClassLoader(ClassLoader classLoader) {
145145
* returning the value found in the bundle as-is (without MessageFormat parsing).
146146
*/
147147
@Override
148+
@Nullable
148149
protected String resolveCodeWithoutArguments(String code, Locale locale) {
149150
Set<String> basenames = getBasenameSet();
150151
for (String basename : basenames) {

spring-context/src/main/java/org/springframework/format/support/FormattingConversionServiceRuntimeHints.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.aot.hint.RuntimeHints;
2020
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2121
import org.springframework.aot.hint.TypeReference;
22+
import org.springframework.lang.Nullable;
2223

2324
/**
2425
* {@link RuntimeHintsRegistrar} to register hints for {@link DefaultFormattingConversionService}.
@@ -29,7 +30,7 @@
2930
class FormattingConversionServiceRuntimeHints implements RuntimeHintsRegistrar {
3031

3132
@Override
32-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
33+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
3334
hints.reflection().registerType(TypeReference.of("javax.money.MonetaryAmount"));
3435
}
3536
}

spring-context/src/main/java/org/springframework/jmx/access/InvalidInvocationException.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import javax.management.JMRuntimeException;
2020

21+
import org.springframework.lang.Nullable;
22+
2123
/**
2224
* Thrown when trying to invoke an operation on a proxy that is not exposed
2325
* by the proxied MBean resource's management interface.
@@ -35,7 +37,7 @@ public class InvalidInvocationException extends JMRuntimeException {
3537
* error message.
3638
* @param msg the detail message
3739
*/
38-
public InvalidInvocationException(String msg) {
40+
public InvalidInvocationException(@Nullable String msg) {
3941
super(msg);
4042
}
4143

spring-context/src/main/java/org/springframework/jmx/access/MBeanProxyFactoryBean.java

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public Object getObject() {
108108
}
109109

110110
@Override
111+
@Nullable
111112
public Class<?> getObjectType() {
112113
return this.proxyInterface;
113114
}

spring-context/src/main/java/org/springframework/jndi/JndiObjectFactoryBean.java

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ public Object getObject() {
273273
}
274274

275275
@Override
276+
@Nullable
276277
public Class<?> getObjectType() {
277278
if (this.proxyInterfaces != null) {
278279
if (this.proxyInterfaces.length == 1) {

spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncConfigurerSupport.java

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
public class AsyncConfigurerSupport implements AsyncConfigurer {
3535

3636
@Override
37+
@Nullable
3738
public Executor getAsyncExecutor() {
3839
return null;
3940
}

spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskScheduler.java

+3
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,19 @@ public LastExecutionAdapter(@Nullable LastExecution le) {
334334
}
335335

336336
@Override
337+
@Nullable
337338
public Instant lastScheduledExecution() {
338339
return (this.le != null ? toInstant(this.le.getScheduledStart()) : null);
339340
}
340341

341342
@Override
343+
@Nullable
342344
public Instant lastActualExecution() {
343345
return (this.le != null ? toInstant(this.le.getRunStart()) : null);
344346
}
345347

346348
@Override
349+
@Nullable
347350
public Instant lastCompletion() {
348351
return (this.le != null ? toInstant(this.le.getRunEnd()) : null);
349352
}

spring-context/src/main/java/org/springframework/scheduling/config/TaskSchedulerRouter.java

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public void setBeanFactory(@Nullable BeanFactory beanFactory) {
106106

107107

108108
@Override
109+
@Nullable
109110
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
110111
return determineTargetScheduler(task).schedule(task, trigger);
111112
}

spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public String getExpression() {
112112
* previous execution; therefore, overlapping executions won't occur.
113113
*/
114114
@Override
115+
@Nullable
115116
public Instant nextExecution(TriggerContext triggerContext) {
116117
Instant timestamp = determineLatestTimestamp(triggerContext);
117118
ZoneId zone = (this.zoneId != null ? this.zoneId : triggerContext.getClock().getZone());

spring-context/src/main/java/org/springframework/scheduling/support/QuartzCronField.java

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ private static Temporal rollbackToMidnight(Temporal current, Temporal result) {
338338

339339

340340
@Override
341+
@Nullable
341342
public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
342343
T result = adjust(temporal);
343344
if (result != null) {

spring-context/src/main/java/org/springframework/scripting/config/ScriptingDefaultsParser.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.beans.factory.config.TypedStringValue;
2323
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2424
import org.springframework.beans.factory.xml.ParserContext;
25+
import org.springframework.lang.Nullable;
2526
import org.springframework.util.StringUtils;
2627

2728
/**
@@ -38,6 +39,7 @@ class ScriptingDefaultsParser implements BeanDefinitionParser {
3839

3940

4041
@Override
42+
@Nullable
4143
public BeanDefinition parse(Element element, ParserContext parserContext) {
4244
BeanDefinition bd =
4345
LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry());

spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, Str
305305
}
306306

307307
@Override
308+
@Nullable
308309
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
309310
// We only apply special treatment to ScriptFactory implementations here.
310311
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {

0 commit comments

Comments
 (0)