From c0550f7eb6c2a669a8bd945c49a16ef2fe8d96a4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 9 Jun 2017 20:46:15 +0200 Subject: [PATCH] Documented AbstractBeanFactory nullability warning Issue: SPR-15540 --- .../support/AbstractBeanDefinitionReader.java | 2 +- .../beans/factory/support/AbstractBeanFactory.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java index bacd7c062f..02f45badd9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java @@ -136,7 +136,7 @@ public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable * with the corresponding Classes to be resolved later (or never). * @see Thread#getContextClassLoader() */ - public void setBeanClassLoader(ClassLoader beanClassLoader) { + public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 5e50772e82..717490afb5 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -365,6 +365,18 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp } // Check if required type matches the type of the actual bean instance. + // Note that the following return declarations are technically violating the + // non-null policy for the getBean methods: However, these will only result + // in null under very specific circumstances: such as a user-declared factory + // method returning null or a user-provided FactoryBean.getObject() returning + // null, without any custom post-processing of such null values. We will pass + // them on as null to corresponding injection points in that exceptional case + // but do not expect user-level getBean callers to deal with such null values. + // In the end, regular getBean callers should be able to assign the outcome + // to non-null variables/arguments without being compromised by rather esoteric + // corner cases, in particular in functional configuration and Kotlin scenarios. + // A future Spring generation might eventually forbid null values completely + // and throw IllegalStateExceptions instead of leniently passing them through. if (requiredType != null && bean != null && !requiredType.isInstance(bean)) { try { return getTypeConverter().convertIfNecessary(bean, requiredType); @@ -377,6 +389,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } + // For the nullability warning, see the elaboration in the comment above. return (T) bean; }