Is there a tool or library that converts object serialized into YAML or XML file into Java code creating that object?
From technical point of view, I see no difficulties here. Both Yaml and XStream (or other similar tools) needs to find Java class name via reflection, find constructor, invoke it, then find setters and invoke them. Reflection invokes can be replaced by Java code generation that is doing the same. It requires a bit work, however.
Does such tool exist? Or maybe someone is developing it and want to share it with public?
P.S. Any library with ability to convert given Java object into Java code would fullfill my needs.
clarification
I expect the tool to convert example YAML:
--- !pl.example.Contact
name: Nathan Sweet
age: 28
Or XML:
<contact>
<name>Nathan Sweet</name>
<age>28</age>
</contact>
into the code:
pl.example.Contact contact = new pl.example.Contact();
contact.setName("Nathan Sweet");
contact.setAge(28);
I’ve asked about converting YAML or XML into Java code, but that what I really needed was to generate Java code that would create given Java object. Reading existing YAML can be done by one of many Java YAML libraries.
However, I wasn’t able to find any library that would create Java code from given Java object. Maybe I was searching for wrong keywords, maybe I’ve finished my search too early, but I haven’t found anything.
I have written that tool myself. The code is quite primitive and probably will not be enough in many cases (it has no support for abstract classes and interfaces etc.), but was good enough for the task I wanted to accomplish. So I post the source code of my solution as the answer to my question: