Skip to content

Commit d4f544d

Browse files
committed
Add missing precondition check to AutowireUtils.resolveDependency
See spring-projectsgh-2060
1 parent 02be21d commit d4f544d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
334334
* that declares the parameter
335335
* @param containingClass the concrete class that contains the parameter; this may
336336
* differ from the class that declares the parameter in that it may be a subclass
337-
* thereof, potentially substituting type variables
337+
* thereof, potentially substituting type variables (must not be {@code null})
338338
* @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
339339
* the dependency (must not be {@code null})
340340
* @return the resolved object, or {@code null} if none found
@@ -351,6 +351,7 @@ public static Object resolveDependency(
351351
throws BeansException {
352352

353353
Assert.notNull(parameter, "Parameter must not be null");
354+
Assert.notNull(containingClass, "Containing class must not be null");
354355
Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
355356

356357
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);

spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,24 @@ public void resolveDependencyPreconditionsForParameter() {
159159
}
160160

161161
@Test
162-
public void resolveDependencyPreconditionsForBeanFactory() throws Exception {
163-
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
164-
Parameter parameter = method.getParameters()[0];
162+
public void resolveDependencyPreconditionsForContainingClass() throws Exception {
163+
exception.expect(IllegalArgumentException.class);
164+
exception.expectMessage("Containing class must not be null");
165+
AutowireUtils.resolveDependency(getParameter(), 0, null, null);
166+
}
165167

168+
@Test
169+
public void resolveDependencyPreconditionsForBeanFactory() throws Exception {
166170
exception.expect(IllegalArgumentException.class);
167171
exception.expectMessage("AutowireCapableBeanFactory must not be null");
168-
AutowireUtils.resolveDependency(parameter, 0, null, null);
172+
AutowireUtils.resolveDependency(getParameter(), 0, getClass(), null);
169173
}
170174

175+
private Parameter getParameter() throws NoSuchMethodException {
176+
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
177+
return method.getParameters()[0];
178+
}
179+
171180
@Test
172181
public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception {
173182
Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);

0 commit comments

Comments
 (0)