Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
HYH.APSJ
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
佟礼
HYH.APSJ
Commits
b762cdb4
Commit
b762cdb4
authored
Nov 26, 2025
by
DESKTOP-VKRD9QF\Administration
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
copy
parent
681b20a8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
+54
-0
DeepCopyUtil.java
src/main/java/com/aps/common/util/DeepCopyUtil.java
+54
-0
No files found.
src/main/java/com/aps/common/util/DeepCopyUtil.java
0 → 100644
View file @
b762cdb4
package
com
.
aps
.
common
.
util
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
java.util.List
;
import
java.util.Optional
;
/**
* 最常用的深拷贝工具类
*/
public
class
DeepCopyUtil
{
private
static
final
ObjectMapper
objectMapper
=
new
ObjectMapper
();
/**
* 单个对象深拷贝(最常用)
*/
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
>
T
deepCopy
(
T
source
)
{
if
(
source
==
null
)
return
null
;
try
{
String
json
=
objectMapper
.
writeValueAsString
(
source
);
return
(
T
)
objectMapper
.
readValue
(
json
,
source
.
getClass
());
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"深拷贝失败"
,
e
);
}
}
/**
* 列表深拷贝(很常用)
*/
public
static
<
T
>
List
<
T
>
deepCopyList
(
List
<
T
>
source
)
{
if
(
source
==
null
)
return
List
.
of
();
try
{
String
json
=
objectMapper
.
writeValueAsString
(
source
);
return
objectMapper
.
readValue
(
json
,
new
TypeReference
<
List
<
T
>>()
{});
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"列表深拷贝失败"
,
e
);
}
}
/**
* 安全深拷贝(返回Optional,避免异常)
*/
public
static
<
T
>
Optional
<
T
>
safeDeepCopy
(
T
source
)
{
try
{
return
Optional
.
ofNullable
(
deepCopy
(
source
));
}
catch
(
Exception
e
)
{
return
Optional
.
empty
();
}
}
}
\ 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