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
e03f3b8e
Commit
e03f3b8e
authored
May 15, 2019
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.1.x'
Closes gh-16857
parents
544e81cc
f665910c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
16 deletions
+64
-16
JavaBeanBinder.java
...ramework/boot/context/properties/bind/JavaBeanBinder.java
+21
-16
JavaBeanBinderTests.java
...ork/boot/context/properties/bind/JavaBeanBinderTests.java
+43
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java
View file @
e03f3b8e
...
...
@@ -102,16 +102,16 @@ class JavaBeanBinder implements BeanBinder {
private
static
Bean
<?>
cached
;
private
final
Class
<?>
type
;
private
final
ResolvableType
type
;
private
final
ResolvableType
resolvable
Type
;
private
final
Class
<?>
resolved
Type
;
private
final
Map
<
String
,
BeanProperty
>
properties
=
new
LinkedHashMap
<>();
Bean
(
ResolvableType
resolvableType
,
Class
<?>
type
)
{
this
.
resolvableType
=
resolvableType
;
Bean
(
ResolvableType
type
,
Class
<?>
resolvedType
)
{
this
.
type
=
type
;
putProperties
(
type
);
this
.
resolvedType
=
resolvedType
;
putProperties
(
resolvedType
);
}
private
void
putProperties
(
Class
<?>
type
)
{
...
...
@@ -155,7 +155,7 @@ class JavaBeanBinder implements BeanBinder {
}
private
BeanProperty
getBeanProperty
(
String
name
)
{
return
new
BeanProperty
(
name
,
this
.
resolvableT
ype
);
return
new
BeanProperty
(
name
,
this
.
t
ype
);
}
private
void
addField
(
Field
field
)
{
...
...
@@ -165,10 +165,6 @@ class JavaBeanBinder implements BeanBinder {
}
}
public
Class
<?>
getType
()
{
return
this
.
type
;
}
public
Map
<
String
,
BeanProperty
>
getProperties
()
{
return
this
.
properties
;
}
...
...
@@ -181,27 +177,36 @@ class JavaBeanBinder implements BeanBinder {
instance
=
target
.
getValue
().
get
();
}
if
(
instance
==
null
)
{
instance
=
(
T
)
BeanUtils
.
instantiateClass
(
this
.
t
ype
);
instance
=
(
T
)
BeanUtils
.
instantiateClass
(
this
.
resolvedT
ype
);
}
return
instance
;
});
}
private
boolean
isOfDifferentType
(
ResolvableType
targetType
)
{
if
(
this
.
type
.
hasGenerics
()
||
targetType
.
hasGenerics
())
{
return
!
this
.
type
.
equals
(
targetType
);
}
return
this
.
resolvedType
==
null
||
!
this
.
resolvedType
.
equals
(
targetType
.
resolve
());
}
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
>
Bean
<
T
>
get
(
Bindable
<
T
>
bindable
,
boolean
canCallGetValue
)
{
Class
<?>
type
=
bindable
.
getType
().
resolve
(
Object
.
class
);
ResolvableType
type
=
bindable
.
getType
();
Class
<?>
resolvedType
=
type
.
resolve
(
Object
.
class
);
Supplier
<
T
>
value
=
bindable
.
getValue
();
T
instance
=
null
;
if
(
canCallGetValue
&&
value
!=
null
)
{
instance
=
value
.
get
();
type
=
(
instance
!=
null
)
?
instance
.
getClass
()
:
t
ype
;
resolvedType
=
(
instance
!=
null
)
?
instance
.
getClass
()
:
resolvedT
ype
;
}
if
(
instance
==
null
&&
!
isInstantiable
(
t
ype
))
{
if
(
instance
==
null
&&
!
isInstantiable
(
resolvedT
ype
))
{
return
null
;
}
Bean
<?>
bean
=
Bean
.
cached
;
if
(
bean
==
null
||
!
type
.
equals
(
bean
.
getType
()
))
{
bean
=
new
Bean
<>(
bindable
.
getType
(),
t
ype
);
if
(
bean
==
null
||
bean
.
isOfDifferentType
(
type
))
{
bean
=
new
Bean
<>(
type
,
resolvedT
ype
);
cached
=
bean
;
}
return
(
Bean
<
T
>)
bean
;
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java
View file @
e03f3b8e
...
...
@@ -494,6 +494,19 @@ public class JavaBeanBinderTests {
assertThat
(
bean
.
getCounter
()).
isEqualTo
(
42
);
}
@Test
public
void
bindToClassShouldCacheWithGenerics
()
{
// gh-16821
MockConfigurationPropertySource
source
=
new
MockConfigurationPropertySource
();
source
.
put
(
"foo.integers[a].value"
,
"1"
);
source
.
put
(
"foo.booleans[b].value"
,
"true"
);
this
.
sources
.
add
(
source
);
ExampleWithGenericMap
bean
=
this
.
binder
.
bind
(
"foo"
,
Bindable
.
of
(
ExampleWithGenericMap
.
class
)).
get
();
assertThat
(
bean
.
getIntegers
().
get
(
"a"
).
getValue
()).
isEqualTo
(
1
);
assertThat
(
bean
.
getBooleans
().
get
(
"b"
).
getValue
()).
isEqualTo
(
true
);
}
public
static
class
ExampleValueBean
{
private
int
intValue
;
...
...
@@ -914,4 +927,34 @@ public class JavaBeanBinderTests {
}
public
static
class
ExampleWithGenericMap
{
private
final
Map
<
String
,
GenericValue
<
Integer
>>
integers
=
new
LinkedHashMap
<>();
private
final
Map
<
String
,
GenericValue
<
Boolean
>>
booleans
=
new
LinkedHashMap
<>();
public
Map
<
String
,
GenericValue
<
Integer
>>
getIntegers
()
{
return
this
.
integers
;
}
public
Map
<
String
,
GenericValue
<
Boolean
>>
getBooleans
()
{
return
this
.
booleans
;
}
}
public
static
class
GenericValue
<
T
>
{
private
T
value
;
public
T
getValue
()
{
return
this
.
value
;
}
public
void
setValue
(
T
value
)
{
this
.
value
=
value
;
}
}
}
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