0

Is it possible to marshall java objects tree to source code, that generates the tree?

What I am trying to do is to create sample data for integration testing from live data. I need to serialize sample objects (i.e. JPA entities) to a human readable form, tweak it and prepare integration tests.

It would be great if something created java code from given object tree.

The structure is quite complex, with two-way references etc. I tried XStream to XML serialization but it fails on two-way references. I tried JAXB too but it seems that it does not support cyclic references at all.

//EDIT:

Actually I've managed to get XStream running. However the question is : Is there a library that can generate a source code from given object tree?

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
  • Not sure why XStream fails? Can you please give a used case? – titogeo Aug 07 '12 at 09:55
  • Are you using default settings for XStream? – titogeo Aug 07 '12 at 10:00
  • From my exp cyclic references you will have to handle separately like nullify the node etc. Buy you can try out some advanced options in Xstream like setMode etc for two-way references. – titogeo Aug 07 '12 at 10:52
  • You could try the `@XmlInverseReference` extension in EclipseLink JAXB (MOXy): http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html – bdoughan Aug 07 '12 at 12:44
  • I've managed to go through references issues and failed on Javassist proxies. – Piotr Gwiazda Aug 07 '12 at 12:51
  • I have successfully done something like this using reflection to automatically create Java source code that sets up objects used for unit tests. In some corner cases, annotations were needed - to be able to deduce generic type parameters, setting up maps and filling them etc. It works well, but we also have not yet had the case of cyclic references. – Axel Aug 07 '12 at 13:50
  • OP: what kind of approach did you finally use? – Cwt Nov 01 '13 at 19:03

0 Answers0