Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
order-management-backend
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
1
Merge Requests
1
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
Ascend
order-management-backend
Commits
10d911ad
Commit
10d911ad
authored
May 11, 2021
by
Alex Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AFP-91]
🎨
Create bean 'fieldCombiner' to copy fields from one class to another (
@asegers
)
parent
c9c621d4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
BeanConfig.java
src/main/java/com/afp/ordermanagement/config/BeanConfig.java
+46
-0
No files found.
src/main/java/com/afp/ordermanagement/config/BeanConfig.java
0 → 100644
View file @
10d911ad
package
com
.
afp
.
ordermanagement
.
config
;
import
com.afp.ordermanagement.model.Manager
;
import
com.github.javafaker.Faker
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
java.lang.reflect.Field
;
import
java.util.Arrays
;
import
static
java
.
util
.
Objects
.
isNull
;
@Configuration
public
class
BeanConfig
{
@Bean
public
static
Faker
faker
()
{
return
new
Faker
();
}
@Bean
public
static
FieldCombiner
fieldCombiner
()
{
return
new
FieldCombiner
();
}
public
static
class
FieldCombiner
{
public
Object
combine
(
Object
target
,
Object
source
,
String
[]
omitFields
)
{
try
{
for
(
Field
field:
Manager
.
class
.
getDeclaredFields
())
{
String
fieldName
=
field
.
getName
();
field
.
setAccessible
(
true
);
Object
sourceValue
=
field
.
get
(
source
);
if
(!
isNull
(
sourceValue
)
&&
Arrays
.
binarySearch
(
omitFields
,
fieldName
)
<=
0
)
field
.
set
(
target
,
sourceValue
);
}
}
catch
(
Exception
ignore
)
{
}
return
target
;
}
public
Object
combine
(
Object
target
,
Object
source
)
{
return
this
.
combine
(
target
,
source
,
new
String
[
0
]);
}
}
}
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