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
0bfa9cf1
Commit
0bfa9cf1
authored
Dec 10, 2020
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.2.x' into 2.3.x
Closes gh-24424
parents
b14847d0
86914176
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
4 deletions
+63
-4
JavaBeanBinder.java
...ramework/boot/context/properties/bind/JavaBeanBinder.java
+12
-3
JavaBeanBinderTests.java
...ork/boot/context/properties/bind/JavaBeanBinderTests.java
+51
-1
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java
View file @
0bfa9cf1
/*
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -21,9 +21,12 @@ import java.lang.annotation.Annotation;
...
@@ -21,9 +21,12 @@ import java.lang.annotation.Annotation;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.util.Arrays
;
import
java.util.Comparator
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.function.BiConsumer
;
import
java.util.function.BiConsumer
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
import
java.util.function.Supplier
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.BeanUtils
;
...
@@ -126,13 +129,19 @@ class JavaBeanBinder implements DataObjectBinder {
...
@@ -126,13 +129,19 @@ class JavaBeanBinder implements DataObjectBinder {
private
void
addProperties
(
Class
<?>
type
)
{
private
void
addProperties
(
Class
<?>
type
)
{
while
(
type
!=
null
&&
!
Object
.
class
.
equals
(
type
))
{
while
(
type
!=
null
&&
!
Object
.
class
.
equals
(
type
))
{
Method
[]
declaredMethods
=
type
.
getDeclaredMethods
(
);
Method
[]
declaredMethods
=
getSorted
(
type
,
Class:
:
getDeclaredMethods
,
Method:
:
getName
);
Field
[]
declaredFields
=
type
.
getDeclaredFields
(
);
Field
[]
declaredFields
=
getSorted
(
type
,
Class:
:
getDeclaredFields
,
Field:
:
getName
);
addProperties
(
declaredMethods
,
declaredFields
);
addProperties
(
declaredMethods
,
declaredFields
);
type
=
type
.
getSuperclass
();
type
=
type
.
getSuperclass
();
}
}
}
}
private
<
S
,
E
>
E
[]
getSorted
(
S
source
,
Function
<
S
,
E
[]>
elements
,
Function
<
E
,
String
>
name
)
{
E
[]
result
=
elements
.
apply
(
source
);
Arrays
.
sort
(
result
,
Comparator
.
comparing
(
name
));
return
result
;
}
protected
void
addProperties
(
Method
[]
declaredMethods
,
Field
[]
declaredFields
)
{
protected
void
addProperties
(
Method
[]
declaredMethods
,
Field
[]
declaredFields
)
{
for
(
int
i
=
0
;
i
<
declaredMethods
.
length
;
i
++)
{
for
(
int
i
=
0
;
i
<
declaredMethods
.
length
;
i
++)
{
if
(!
isCandidate
(
declaredMethods
[
i
]))
{
if
(!
isCandidate
(
declaredMethods
[
i
]))
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java
View file @
0bfa9cf1
/*
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -26,6 +26,7 @@ import java.util.LinkedHashSet;
...
@@ -26,6 +26,7 @@ import java.util.LinkedHashSet;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
...
@@ -540,6 +541,19 @@ class JavaBeanBinderTests {
...
@@ -540,6 +541,19 @@ class JavaBeanBinderTests {
assertThat
(
bean
.
getProperty
()).
isEqualTo
(
"test"
);
assertThat
(
bean
.
getProperty
()).
isEqualTo
(
"test"
);
}
}
@Test
void
bindUsesConsistentPropertyOrder
()
{
MockConfigurationPropertySource
source
=
new
MockConfigurationPropertySource
();
source
.
put
(
"foo.gamma"
,
"0"
);
source
.
put
(
"foo.alpha"
,
"0"
);
source
.
put
(
"foo.beta"
,
"0"
);
this
.
sources
.
add
(
source
);
PropertyOrderBean
bean
=
this
.
binder
.
bind
(
"foo"
,
Bindable
.
of
(
PropertyOrderBean
.
class
)).
get
();
assertThat
(
bean
.
getAlpha
()).
isEqualTo
(
0
);
assertThat
(
bean
.
getBeta
()).
isEqualTo
(
1
);
assertThat
(
bean
.
getGamma
()).
isEqualTo
(
2
);
}
static
class
ExampleValueBean
{
static
class
ExampleValueBean
{
private
int
intValue
;
private
int
intValue
;
...
@@ -1036,4 +1050,40 @@ class JavaBeanBinderTests {
...
@@ -1036,4 +1050,40 @@ class JavaBeanBinderTests {
}
}
static
class
PropertyOrderBean
{
static
AtomicInteger
atomic
=
new
AtomicInteger
();
private
int
alpha
;
private
int
beta
;
private
int
gamma
;
int
getAlpha
()
{
return
this
.
alpha
;
}
void
setAlpha
(
int
alpha
)
{
this
.
alpha
=
alpha
+
atomic
.
getAndIncrement
();
}
int
getBeta
()
{
return
this
.
beta
;
}
void
setBeta
(
int
beta
)
{
this
.
beta
=
beta
+
atomic
.
getAndIncrement
();
}
int
getGamma
()
{
return
this
.
gamma
;
}
void
setGamma
(
int
gamma
)
{
this
.
gamma
=
gamma
+
atomic
.
getAndIncrement
();
}
}
}
}
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