From 46a89d9534cbb5596a2754a7655f678b27d504da Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 29 May 2018 21:51:06 +0200 Subject: [PATCH] Restore lenient null return value for ConditionContext.getBeanFactory() Includes nullable return value for getClassLoader() with corresponding notes in applicable javadoc. Issue: SPR-16866 --- .../factory/config/ConfigurableBeanFactory.java | 6 ++++-- .../context/annotation/ConditionContext.java | 14 +++++++++++--- .../context/annotation/ConditionEvaluator.java | 6 +++--- .../springframework/core/io/ResourceLoader.java | 4 +++- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java index 7ffc4f5000..a4f4b6b733 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -89,7 +89,9 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single void setBeanClassLoader(@Nullable ClassLoader beanClassLoader); /** - * Return this factory's class loader for loading bean classes. + * Return this factory's class loader for loading bean classes + * (only {@code null} if even the system ClassLoader isn't accessible). + * @see org.springframework.util.ClassUtils#forName(String, ClassLoader) */ @Nullable ClassLoader getBeanClassLoader(); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConditionContext.java b/spring-context/src/main/java/org/springframework/context/annotation/ConditionContext.java index dcfd17c4a8..79529d1093 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConditionContext.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConditionContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; +import org.springframework.lang.Nullable; /** * Context information for use by {@link Condition}s. @@ -33,13 +34,17 @@ public interface ConditionContext { /** * Return the {@link BeanDefinitionRegistry} that will hold the bean definition * should the condition match. + * @throws IllegalStateException if no registry is available (which is unusual: + * only the case with a plain {@link ClassPathScanningCandidateComponentProvider}) */ BeanDefinitionRegistry getRegistry(); /** * Return the {@link ConfigurableListableBeanFactory} that will hold the bean - * definition should the condition match. + * definition should the condition match, or {@code null} if the bean factory is + * not available (or not downcastable to {@code ConfigurableListableBeanFactory}). */ + @Nullable ConfigurableListableBeanFactory getBeanFactory(); /** @@ -53,8 +58,11 @@ public interface ConditionContext { ResourceLoader getResourceLoader(); /** - * Return the {@link ClassLoader} that should be used to load additional classes. + * Return the {@link ClassLoader} that should be used to load additional classes + * (only {@code null} if even the system ClassLoader isn't accessible). + * @see org.springframework.util.ClassUtils#forName(String, ClassLoader) */ + @Nullable ClassLoader getClassLoader(); } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConditionEvaluator.java b/spring-context/src/main/java/org/springframework/context/annotation/ConditionEvaluator.java index 59fd688de8..6837bf766c 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConditionEvaluator.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConditionEvaluator.java @@ -120,7 +120,7 @@ class ConditionEvaluator { return (List) (values != null ? values : Collections.emptyList()); } - private Condition getCondition(String conditionClassName, ClassLoader classloader) { + private Condition getCondition(String conditionClassName, @Nullable ClassLoader classloader) { Class conditionClass = ClassUtils.resolveClassName(conditionClassName, classloader); return (Condition) BeanUtils.instantiateClass(conditionClass); } @@ -202,8 +202,8 @@ class ConditionEvaluator { } @Override + @Nullable public ConfigurableListableBeanFactory getBeanFactory() { - Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory available"); return this.beanFactory; } @@ -218,8 +218,8 @@ class ConditionEvaluator { } @Override + @Nullable public ClassLoader getClassLoader() { - Assert.state(this.classLoader != null, "No ClassLoader available"); return this.classLoader; } } diff --git a/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java b/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java index c2edc5f579..8b38470b48 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,9 @@ public interface ResourceLoader { * in a uniform manner with the ResourceLoader, rather than relying * on the thread context ClassLoader. * @return the ClassLoader + * (only {@code null} if even the system ClassLoader isn't accessible) * @see org.springframework.util.ClassUtils#getDefaultClassLoader() + * @see org.springframework.util.ClassUtils#forName(String, ClassLoader) */ @Nullable ClassLoader getClassLoader();