Skip to content

Commit c0550f7

Browse files
committed
Documented AbstractBeanFactory nullability warning
Issue: SPR-15540
1 parent a1ce324 commit c0550f7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public ResourceLoader getResourceLoader() {
136136
* with the corresponding Classes to be resolved later (or never).
137137
* @see Thread#getContextClassLoader()
138138
*/
139-
public void setBeanClassLoader(ClassLoader beanClassLoader) {
139+
public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) {
140140
this.beanClassLoader = beanClassLoader;
141141
}
142142

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ public Object getObject() throws BeansException {
365365
}
366366

367367
// Check if required type matches the type of the actual bean instance.
368+
// Note that the following return declarations are technically violating the
369+
// non-null policy for the getBean methods: However, these will only result
370+
// in null under very specific circumstances: such as a user-declared factory
371+
// method returning null or a user-provided FactoryBean.getObject() returning
372+
// null, without any custom post-processing of such null values. We will pass
373+
// them on as null to corresponding injection points in that exceptional case
374+
// but do not expect user-level getBean callers to deal with such null values.
375+
// In the end, regular getBean callers should be able to assign the outcome
376+
// to non-null variables/arguments without being compromised by rather esoteric
377+
// corner cases, in particular in functional configuration and Kotlin scenarios.
378+
// A future Spring generation might eventually forbid null values completely
379+
// and throw IllegalStateExceptions instead of leniently passing them through.
368380
if (requiredType != null && bean != null && !requiredType.isInstance(bean)) {
369381
try {
370382
return getTypeConverter().convertIfNecessary(bean, requiredType);
@@ -377,6 +389,7 @@ public Object getObject() throws BeansException {
377389
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
378390
}
379391
}
392+
// For the nullability warning, see the elaboration in the comment above.
380393
return (T) bean;
381394
}
382395

0 commit comments

Comments
 (0)