Skip to content

Commit 46a89d9

Browse files
committed
Restore lenient null return value for ConditionContext.getBeanFactory()
Includes nullable return value for getClassLoader() with corresponding notes in applicable javadoc. Issue: SPR-16866
1 parent 5bbeade commit 46a89d9

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java

Lines changed: 4 additions & 2 deletions
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-2018 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.
@@ -89,7 +89,9 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single
8989
void setBeanClassLoader(@Nullable ClassLoader beanClassLoader);
9090

9191
/**
92-
* Return this factory's class loader for loading bean classes.
92+
* Return this factory's class loader for loading bean classes
93+
* (only {@code null} if even the system ClassLoader isn't accessible).
94+
* @see org.springframework.util.ClassUtils#forName(String, ClassLoader)
9395
*/
9496
@Nullable
9597
ClassLoader getBeanClassLoader();

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

Lines changed: 11 additions & 3 deletions
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-2018 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.
@@ -20,6 +20,7 @@
2020
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2121
import org.springframework.core.env.Environment;
2222
import org.springframework.core.io.ResourceLoader;
23+
import org.springframework.lang.Nullable;
2324

2425
/**
2526
* Context information for use by {@link Condition}s.
@@ -33,13 +34,17 @@ public interface ConditionContext {
3334
/**
3435
* Return the {@link BeanDefinitionRegistry} that will hold the bean definition
3536
* should the condition match.
37+
* @throws IllegalStateException if no registry is available (which is unusual:
38+
* only the case with a plain {@link ClassPathScanningCandidateComponentProvider})
3639
*/
3740
BeanDefinitionRegistry getRegistry();
3841

3942
/**
4043
* Return the {@link ConfigurableListableBeanFactory} that will hold the bean
41-
* definition should the condition match.
44+
* definition should the condition match, or {@code null} if the bean factory is
45+
* not available (or not downcastable to {@code ConfigurableListableBeanFactory}).
4246
*/
47+
@Nullable
4348
ConfigurableListableBeanFactory getBeanFactory();
4449

4550
/**
@@ -53,8 +58,11 @@ public interface ConditionContext {
5358
ResourceLoader getResourceLoader();
5459

5560
/**
56-
* Return the {@link ClassLoader} that should be used to load additional classes.
61+
* Return the {@link ClassLoader} that should be used to load additional classes
62+
* (only {@code null} if even the system ClassLoader isn't accessible).
63+
* @see org.springframework.util.ClassUtils#forName(String, ClassLoader)
5764
*/
65+
@Nullable
5866
ClassLoader getClassLoader();
5967

6068
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
120120
return (List<String[]>) (values != null ? values : Collections.emptyList());
121121
}
122122

123-
private Condition getCondition(String conditionClassName, ClassLoader classloader) {
123+
private Condition getCondition(String conditionClassName, @Nullable ClassLoader classloader) {
124124
Class<?> conditionClass = ClassUtils.resolveClassName(conditionClassName, classloader);
125125
return (Condition) BeanUtils.instantiateClass(conditionClass);
126126
}
@@ -202,8 +202,8 @@ public BeanDefinitionRegistry getRegistry() {
202202
}
203203

204204
@Override
205+
@Nullable
205206
public ConfigurableListableBeanFactory getBeanFactory() {
206-
Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory available");
207207
return this.beanFactory;
208208
}
209209

@@ -218,8 +218,8 @@ public ResourceLoader getResourceLoader() {
218218
}
219219

220220
@Override
221+
@Nullable
221222
public ClassLoader getClassLoader() {
222-
Assert.state(this.classLoader != null, "No ClassLoader available");
223223
return this.classLoader;
224224
}
225225
}

spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java

Lines changed: 3 additions & 1 deletion
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-2018 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.
@@ -72,7 +72,9 @@ public interface ResourceLoader {
7272
* in a uniform manner with the ResourceLoader, rather than relying
7373
* on the thread context ClassLoader.
7474
* @return the ClassLoader
75+
* (only {@code null} if even the system ClassLoader isn't accessible)
7576
* @see org.springframework.util.ClassUtils#getDefaultClassLoader()
77+
* @see org.springframework.util.ClassUtils#forName(String, ClassLoader)
7678
*/
7779
@Nullable
7880
ClassLoader getClassLoader();

0 commit comments

Comments
 (0)