Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
6c629369
Commit
6c629369
authored
May 23, 2017
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for binding to immutable collection
Fixes gh-9290
parent
db9ec87b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
3 deletions
+32
-3
CollectionBinder.java
...mework/boot/context/properties/bind/CollectionBinder.java
+20
-3
CollectionBinderTests.java
...k/boot/context/properties/bind/CollectionBinderTests.java
+12
-0
No files found.
spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java
View file @
6c629369
...
@@ -51,9 +51,26 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
...
@@ -51,9 +51,26 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
@Override
@Override
protected
Collection
<
Object
>
merge
(
Collection
<
Object
>
existing
,
protected
Collection
<
Object
>
merge
(
Collection
<
Object
>
existing
,
Collection
<
Object
>
additional
)
{
Collection
<
Object
>
additional
)
{
existing
.
clear
();
try
{
existing
.
addAll
(
additional
);
existing
.
clear
();
return
existing
;
existing
.
addAll
(
additional
);
return
existing
;
}
catch
(
UnsupportedOperationException
ex
)
{
return
createNewCollection
(
additional
);
}
}
@SuppressWarnings
(
"unchecked"
)
private
Collection
<
Object
>
createNewCollection
(
Collection
<
Object
>
additional
)
{
try
{
Collection
<
Object
>
merged
=
additional
.
getClass
().
newInstance
();
merged
.
addAll
(
additional
);
return
merged
;
}
catch
(
Exception
e
)
{
throw
new
IllegalStateException
(
"Adding bound values to collection failed."
);
}
}
}
}
}
spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java
View file @
6c629369
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
.
context
.
properties
.
bind
;
package
org
.
springframework
.
boot
.
context
.
properties
.
bind
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.LinkedList
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -289,4 +290,15 @@ public class CollectionBinderTests {
...
@@ -289,4 +290,15 @@ public class CollectionBinderTests {
List
<
String
>
values
=
result
.
stream
().
map
(
JavaBean:
:
getValue
).
collect
(
Collectors
.
toList
());
List
<
String
>
values
=
result
.
stream
().
map
(
JavaBean:
:
getValue
).
collect
(
Collectors
.
toList
());
assertThat
(
values
).
containsExactly
(
"a"
,
"b"
,
"c"
);
assertThat
(
values
).
containsExactly
(
"a"
,
"b"
,
"c"
);
}
}
@Test
public
void
bindToImmutableCollectionShouldReturnPopulatedCollection
()
throws
Exception
{
MockConfigurationPropertySource
source
=
new
MockConfigurationPropertySource
();
source
.
put
(
"foo.values"
,
"a,b,c"
);
this
.
sources
.
add
(
source
);
Set
<
String
>
result
=
this
.
binder
.
bind
(
"foo.values"
,
STRING_SET
.
withExistingValue
(
Collections
.
emptySet
())).
get
();
assertThat
(
result
).
hasSize
(
3
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment