Skip to content

Commit 244e62e

Browse files
committed
fixed CopyBuilder to use the shallowCopy method of Copyable
1 parent 6ece691 commit 244e62e

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/main/java/org/xmlobjects/util/copy/CopyBuilder.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ private <T> T copy(T src, T dest, Class<T> template, boolean shallowCopy) {
130130
isCloning = true;
131131
}
132132

133-
if (template == null) {
134-
template = (Class<T>) src.getClass();
135-
}
136-
137133
// avoid copying parents not belonging to the hierarchy of the initial source object
138134
if (src instanceof Child child) {
139135
Child parent = child.getParent();
@@ -145,7 +141,16 @@ private <T> T copy(T src, T dest, Class<T> template, boolean shallowCopy) {
145141
T clone = (T) clones.get(src);
146142
try {
147143
if (clone == null) {
148-
try {
144+
if (shallowCopy
145+
&& isInitial
146+
&& dest == null
147+
&& src instanceof Copyable copyable) {
148+
clone = (T) copyable.shallowCopy(this);
149+
} else {
150+
if (template == null) {
151+
template = (Class<T>) src.getClass();
152+
}
153+
149154
AbstractCloner<T> cloner = (AbstractCloner<T>) findCloner(template);
150155
if (cloner != IDENTITY_CLONER && cloner != NULL_CLONER) {
151156
if (dest == null) {
@@ -156,18 +161,16 @@ private <T> T copy(T src, T dest, Class<T> template, boolean shallowCopy) {
156161
}
157162

158163
clone = cloner.copy(src, dest, shallowCopy);
159-
} catch (Throwable e) {
160-
if (failOnError) {
161-
throw e instanceof CopyException copyException ?
162-
copyException :
163-
new CopyException("Failed to copy " + src + ".", e);
164-
}
165164
}
166165
} else if (clone == NULL) {
167166
clone = null;
168167
}
169-
170-
return clone;
168+
} catch (Throwable e) {
169+
if (failOnError) {
170+
throw e instanceof CopyException copyException ?
171+
copyException :
172+
new CopyException("Failed to copy " + src + ".", e);
173+
}
171174
} finally {
172175
if (isInitial) {
173176
isCloning = false;
@@ -179,6 +182,8 @@ private <T> T copy(T src, T dest, Class<T> template, boolean shallowCopy) {
179182
}
180183
}
181184
}
185+
186+
return clone;
182187
}
183188

184189
private AbstractCloner<?> findCloner(Class<?> type) {

0 commit comments

Comments
 (0)