Generic matching for ObjectProvider stream and empty vararg resolution

Issue: SPR-11419
Issue: SPR-15338
This commit is contained in:
Juergen Hoeller
2018-07-24 13:03:54 +02:00
parent 6372c0f47c
commit a9c9ba6601
5 changed files with 143 additions and 725 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.beans.factory.support;
import java.beans.ConstructorProperties;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
@@ -824,9 +825,12 @@ class ConstructorResolver {
}
catch (NoSuchBeanDefinitionException ex) {
if (fallback) {
// Single constructor or factory method -> let's return an
// empty collection for a non-null collection parameter.
if (CollectionFactory.isApproximableCollectionType(paramType)) {
// Single constructor or factory method -> let's return an empty array/collection
// for e.g. a vararg or a non-null List/Set/Map parameter.
if (paramType.isArray()) {
return Array.newInstance(paramType.getComponentType(), 0);
}
else if (CollectionFactory.isApproximableCollectionType(paramType)) {
return CollectionFactory.createCollection(paramType, 0);
}
else if (CollectionFactory.isApproximableMapType(paramType)) {

View File

@@ -1229,7 +1229,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (descriptor.isStreamAccess()) {
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type,
new MultiElementDescriptor(descriptor));
new MultiElementDescriptor(descriptor, false));
if (autowiredBeanNames != null) {
autowiredBeanNames.addAll(matchingBeans.keySet());
}
@@ -1247,7 +1247,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return null;
}
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType,
new MultiElementDescriptor(descriptor));
new MultiElementDescriptor(descriptor, true));
if (matchingBeans.isEmpty()) {
return null;
}
@@ -1267,7 +1267,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return null;
}
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
new MultiElementDescriptor(descriptor));
new MultiElementDescriptor(descriptor, true));
if (matchingBeans.isEmpty()) {
return null;
}
@@ -1292,7 +1292,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return null;
}
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, valueType,
new MultiElementDescriptor(descriptor));
new MultiElementDescriptor(descriptor, true));
if (matchingBeans.isEmpty()) {
return null;
}
@@ -1716,10 +1716,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
/**
* A dependency descriptor marker for multiple elements.
*/
private static class MultiElementDescriptor extends NestedDependencyDescriptor {
private static class MultiElementDescriptor extends DependencyDescriptor {
public MultiElementDescriptor(DependencyDescriptor original) {
public MultiElementDescriptor(DependencyDescriptor original, boolean nested) {
super(original);
if (nested) {
increaseNestingLevel();
}
}
}