Skip to content

Commit

Permalink
修复Tree.cloneTree的Parent节点引用错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Aug 30, 2024
1 parent baac5d0 commit c3b576f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* 【crypto 】 修复ZipUtil压缩成流的方法检查文件时报错问题(issue#3697@Github)
* 【core 】 修复CopyOptions.setFieldValueEditor后生成null值setIgnoreNullValue无效问题(issue#3702@Github)
* 【json 】 修复JSONConfig.setDateFormat设置后setWriteLongAsString失效问题(issue#IALQ0N@Gitee)
* 【core 】 修复Tree.cloneTree的Parent节点引用错误问题(issue#IANJTC@Gitee)

-------------------------------------------------------------------------------------------------------------
# 5.8.31(2024-08-12)
Expand Down
9 changes: 6 additions & 3 deletions hutool-core/src/main/java/cn/hutool/core/lang/tree/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,22 +315,25 @@ public String toString() {
*/
public Tree<T> cloneTree() {
final Tree<T> result = ObjectUtil.clone(this);
result.setChildren(cloneChildren());
result.setChildren(cloneChildren(result));
return result;
}

/**
* 递归复制子节点
*
* @param parent 新的父节点
* @return 新的子节点列表
*/
private List<Tree<T>> cloneChildren() {
private List<Tree<T>> cloneChildren(final Tree<T> parent) {
final List<Tree<T>> children = getChildren();
if (null == children) {
return null;
}
final List<Tree<T>> newChildren = new ArrayList<>(children.size());
children.forEach((t) -> newChildren.add(t.cloneTree()));
children.forEach((t) -> {
newChildren.add(t.cloneTree().setParent(parent));
});
return newChildren;
}

Expand Down

0 comments on commit c3b576f

Please sign in to comment.