Created
          February 10, 2011 21:45 
        
      - 
      
- 
        Save darrend/821410 to your computer and use it in GitHub Desktop. 
    deep copy an object using jaxb
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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()); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Thanks a lot! It really helps