Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
panwangnan
schbrain-parent
Commits
a1c2069a
Commit
a1c2069a
authored
May 02, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BeanCopyUtils support property type convert
parent
d210f6a0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
2 deletions
+38
-2
commons/common-util/src/main/java/com/schbrain/common/util/BeanCopyUtils.java
...src/main/java/com/schbrain/common/util/BeanCopyUtils.java
+38
-2
No files found.
commons/common-util/src/main/java/com/schbrain/common/util/BeanCopyUtils.java
View file @
a1c2069a
...
@@ -2,9 +2,14 @@ package com.schbrain.common.util;
...
@@ -2,9 +2,14 @@ package com.schbrain.common.util;
import
cn.hutool.core.lang.Singleton
;
import
cn.hutool.core.lang.Singleton
;
import
cn.hutool.core.util.ReflectUtil
;
import
cn.hutool.core.util.ReflectUtil
;
import
cn.hutool.extra.spring.SpringUtil
;
import
com.google.common.base.Joiner
;
import
com.google.common.base.Joiner
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.beans.BeansException
;
import
org.springframework.cglib.beans.BeanCopier
;
import
org.springframework.cglib.beans.BeanCopier
;
import
org.springframework.cglib.core.Converter
;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.util.ClassUtils
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -19,6 +24,8 @@ public class BeanCopyUtils {
...
@@ -19,6 +24,8 @@ public class BeanCopyUtils {
private
static
final
Joiner
CACHE_KEY_JOINER
=
Joiner
.
on
(
"#"
);
private
static
final
Joiner
CACHE_KEY_JOINER
=
Joiner
.
on
(
"#"
);
private
static
final
ConversionServiceConverter
CONVERTER
=
new
ConversionServiceConverter
();
/**
/**
* copy object list
* copy object list
*/
*/
...
@@ -47,17 +54,46 @@ public class BeanCopyUtils {
...
@@ -47,17 +54,46 @@ public class BeanCopyUtils {
return
null
;
return
null
;
}
}
BeanCopier
copier
=
getCopier
(
source
.
getClass
(),
target
.
getClass
());
BeanCopier
copier
=
getCopier
(
source
.
getClass
(),
target
.
getClass
());
copier
.
copy
(
source
,
target
,
null
);
copier
.
copy
(
source
,
target
,
CONVERTER
);
return
target
;
return
target
;
}
}
private
static
BeanCopier
getCopier
(
Class
<?>
sourceClass
,
Class
<?>
targetClass
)
{
private
static
BeanCopier
getCopier
(
Class
<?>
sourceClass
,
Class
<?>
targetClass
)
{
String
cacheKey
=
buildCacheKey
(
sourceClass
,
targetClass
);
String
cacheKey
=
buildCacheKey
(
sourceClass
,
targetClass
);
return
Singleton
.
get
(
cacheKey
,
()
->
BeanCopier
.
create
(
sourceClass
,
targetClass
,
fals
e
));
return
Singleton
.
get
(
cacheKey
,
()
->
BeanCopier
.
create
(
sourceClass
,
targetClass
,
tru
e
));
}
}
private
static
String
buildCacheKey
(
Class
<?>
source
,
Class
<?>
target
)
{
private
static
String
buildCacheKey
(
Class
<?>
source
,
Class
<?>
target
)
{
return
CACHE_KEY_JOINER
.
join
(
source
.
getName
(),
target
.
getName
());
return
CACHE_KEY_JOINER
.
join
(
source
.
getName
(),
target
.
getName
());
}
}
@SuppressWarnings
(
"unchecked"
)
private
static
class
ConversionServiceConverter
implements
Converter
{
private
ConversionService
conversionService
;
private
ConversionServiceConverter
()
{
try
{
this
.
conversionService
=
SpringUtil
.
getBean
(
ConversionService
.
class
);
}
catch
(
BeansException
e
)
{
this
.
conversionService
=
null
;
}
}
@Override
public
Object
convert
(
Object
value
,
Class
targetType
,
Object
context
)
{
if
(
value
==
null
)
{
return
null
;
}
if
(
ClassUtils
.
isAssignableValue
(
targetType
,
value
))
{
return
value
;
}
if
(
conversionService
!=
null
&&
conversionService
.
canConvert
(
value
.
getClass
(),
targetType
))
{
return
conversionService
.
convert
(
value
,
targetType
);
}
return
value
;
}
}
}
}
\ No newline at end of file
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