Clone A JAXB Object https://stackoverflow.com/questions/930840/how-do-i-clone-a-jaxb-object
public <T extends Serializable> T clone(T jaxbObject) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(out);
o.writeObject(jaxbObject);
o.flush();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream i = new ObjectInputStream(in);
return (T) i.readObject();
}
public <T extends Serializable> T clone(T jaxbObject) {
StringWriter xml = new StringWriter();
JAXB.marshal(jaxbObject, xml);
StringReader reader = new StringReader(xml.toString());
return JAXB.unmarshal(reader, jaxbObject.getClass());
}
public static <T> T deepCopyJAXB(T object, Class<T> clazz) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, object);
JAXBSource source = new JAXBSource(jaxbContext, contentObject);
return jaxbContext.createUnmarshaller().unmarshal(source, clazz).getValue();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public static <T> T deepCopyJAXB(T object) {
if(object==null) throw new RuntimeException("Can't guess at class");
return deepCopyJAXB(object, (Class<T>) object.getClass());
}