Browse Source

Add missing precondition check to AutowireUtils.resolveDependency

See gh-2060
pull/22500/head
Sam Brannen 7 years ago
parent
commit
d4f544d42f
  1. 3
      spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java
  2. 17
      spring-beans/src/test/java/org/springframework/beans/factory/support/AutowireUtilsTests.java

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

@ -334,7 +334,7 @@ public abstract class AutowireUtils { @@ -334,7 +334,7 @@ public abstract class AutowireUtils {
* that declares the parameter
* @param containingClass the concrete class that contains the parameter; this may
* differ from the class that declares the parameter in that it may be a subclass
* thereof, potentially substituting type variables
* thereof, potentially substituting type variables (must not be {@code null})
* @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
* the dependency (must not be {@code null})
* @return the resolved object, or {@code null} if none found
@ -351,6 +351,7 @@ public abstract class AutowireUtils { @@ -351,6 +351,7 @@ public abstract class AutowireUtils {
throws BeansException {
Assert.notNull(parameter, "Parameter must not be null");
Assert.notNull(containingClass, "Containing class must not be null");
Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);

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

@ -159,15 +159,24 @@ public class AutowireUtilsTests { @@ -159,15 +159,24 @@ public class AutowireUtilsTests {
}
@Test
public void resolveDependencyPreconditionsForBeanFactory() throws Exception {
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
Parameter parameter = method.getParameters()[0];
public void resolveDependencyPreconditionsForContainingClass() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Containing class must not be null");
AutowireUtils.resolveDependency(getParameter(), 0, null, null);
}
@Test
public void resolveDependencyPreconditionsForBeanFactory() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("AutowireCapableBeanFactory must not be null");
AutowireUtils.resolveDependency(parameter, 0, null, null);
AutowireUtils.resolveDependency(getParameter(), 0, getClass(), null);
}
private Parameter getParameter() throws NoSuchMethodException {
Method method = getClass().getDeclaredMethod("autowirableMethod", String.class, String.class, String.class, String.class);
return method.getParameters()[0];
}
@Test
public void resolveDependencyForAnnotatedParametersInTopLevelClassConstructor() throws Exception {
Constructor<?> constructor = AutowirableClass.class.getConstructor(String.class, String.class, String.class, String.class);

Loading…
Cancel
Save