diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ArrayBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ArrayBinder.java index 70dac910326..ff4299dc03e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ArrayBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ArrayBinder.java @@ -38,12 +38,12 @@ class ArrayBinder extends IndexedElementsBinder { @Override protected Object bindAggregate(ConfigurationPropertyName name, Bindable target, AggregateElementBinder elementBinder) { - IndexedCollectionSupplier collection = new IndexedCollectionSupplier( - ArrayList::new); + IndexedCollectionSupplier result = new IndexedCollectionSupplier(ArrayList::new); + ResolvableType aggregateType = target.getType(); ResolvableType elementType = target.getType().getComponentType(); - bindIndexed(name, elementBinder, collection, target.getType(), elementType); - if (collection.wasSupplied()) { - List list = (List) collection.get(); + bindIndexed(name, elementBinder, aggregateType, elementType, result); + if (result.wasSupplied()) { + List list = (List) result.get(); Object array = Array.newInstance(elementType.resolve(), list.size()); for (int i = 0; i < list.size(); i++) { Array.set(array, i, list.get(i)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java index dd52f16201b..8a14562a61e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java @@ -40,14 +40,14 @@ class CollectionBinder extends IndexedElementsBinder> { AggregateElementBinder elementBinder) { Class collectionType = (target.getValue() == null ? target.getType().resolve() : List.class); - IndexedCollectionSupplier collection = new IndexedCollectionSupplier( - () -> CollectionFactory.createCollection(collectionType, 0)); - ResolvableType elementType = target.getType().asCollection().getGeneric(); ResolvableType aggregateType = ResolvableType.forClassWithGenerics(List.class, target.getType().asCollection().getGenerics()); - bindIndexed(name, elementBinder, collection, aggregateType, elementType); - if (collection.wasSupplied()) { - return collection.get(); + ResolvableType elementType = target.getType().asCollection().getGeneric(); + IndexedCollectionSupplier result = new IndexedCollectionSupplier( + () -> CollectionFactory.createCollection(collectionType, 0)); + bindIndexed(name, elementBinder, aggregateType, elementType, result); + if (result.wasSupplied()) { + return result.get(); } return null; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/IndexedElementsBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/IndexedElementsBinder.java index 8b568266583..7faa52982e3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/IndexedElementsBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/IndexedElementsBinder.java @@ -53,13 +53,20 @@ abstract class IndexedElementsBinder extends AggregateBinder { return source == null || source instanceof IterableConfigurationPropertySource; } + /** + * Bind indexed elements to the supplied collection. + * @param name The name of the property to bind + * @param elementBinder the binder to use for elements + * @param aggregateType the aggregate type, may be a collection or an array + * @param elementType the element type + * @param result the destination for results + */ protected final void bindIndexed(ConfigurationPropertyName name, - AggregateElementBinder elementBinder, IndexedCollectionSupplier collection, - ResolvableType aggregateType, ResolvableType elementType) { + AggregateElementBinder elementBinder, ResolvableType aggregateType, + ResolvableType elementType, IndexedCollectionSupplier result) { for (ConfigurationPropertySource source : getContext().getSources()) { - bindIndexed(source, name, elementBinder, collection, aggregateType, - elementType); - if (collection.wasSupplied() && collection.get() != null) { + bindIndexed(source, name, elementBinder, result, aggregateType, elementType); + if (result.wasSupplied() && result.get() != null) { return; } } @@ -134,7 +141,7 @@ abstract class IndexedElementsBinder extends AggregateBinder { } /** - * {@link AggregateBinder.AggregateSupplier AggregateSupplier} for an index + * {@link AggregateBinder.AggregateSupplier AggregateSupplier} for an indexed * collection. */ protected static class IndexedCollectionSupplier