Skip to content

Commit

Permalink
Avoid doing any xstream related work unless absolutely necessary
Browse files Browse the repository at this point in the history
Relates to: #10081
  • Loading branch information
geoand committed Jun 22, 2020
1 parent 25c7a2a commit 8394406
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
*/
public class XStreamDeepClone implements DeepClone {

private final XStream xStream;
private final Supplier<XStream> xStreamSupplier;

public XStreamDeepClone(ClassLoader classLoader) {
xStream = new XStream();
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByRegExp(new String[] { ".*" });
xStream.setClassLoader(classLoader);
// avoid doing any work eagerly since the cloner is rarely used
xStreamSupplier = () -> {
XStream result = new XStream();
XStream.setupDefaultSecurity(result);
result.allowTypesByRegExp(new String[] { ".*" });
result.setClassLoader(classLoader);
return result;
};
}

public Object clone(Object objectToClone) {
Expand All @@ -40,6 +44,7 @@ public Object get() {
}

private Object doClone(Object objectToClone) {
XStream xStream = xStreamSupplier.get();
final String serialized = xStream.toXML(objectToClone);
final Object result = xStream.fromXML(serialized);
if (result == null) {
Expand Down

0 comments on commit 8394406

Please sign in to comment.