I have multiple packages with similar JAXB JAVA objects. The java objects are automatically generated from different XSDs and seem to have a similar structure but I cannot assume that there will be no difference and I cannot marshal two similar XML documents to the same JAVA classes even though they have a lot of similarities.
I am trying to find a way to reduce code duplication and not write the same code again for each package with the same objects when fetching the data from similar objects but different packages.
public void function1(JAXBElement documentJAXB)
{
ObjType obj = new ObjType ();
// parse the document
path.to.package1.Document doc = (path.to.package1.Document) documentJAXB.getValue();
obj.setMsgid(doc.getFIToFIPmtStsRpt().getGrpHdr().getMsgId());
obj.setCredtm(Util.getSqlDate(doc.getFIToFIPmtStsRpt().getGrpHdr().getCreDtTm()));
....
}
The second function applies the same behavior to similar object under package2
public void function2(JAXBElement documentJAXB)
{
ObjType obj = new ObjType ();
// parse the document
path.to.package2.Document doc = (path.to.package2.Document) documentJAXB.getValue();
obj.setMsgid(doc.getFIToFIPmtStsRpt().getGrpHdr().getMsgId());
obj.setCredtm(Util.getSqlDate(doc.getFIToFIPmtStsRpt().getGrpHdr().getCreDtTm()));
....
}
Is there a way to combine this code? I thought of generics but it fails to compile function calls getFIToFIPmtStsRpt
for example since there is no interface that these classes implement.
Is there a way to define the package name dynamically?