I've got the following JAXB unmarshalling code which I'm trying to generalize. Here's what it looks like:
private Object getResponseObject(String stubbedXmlFile,
Class jaxbInterfaceClass,
AbstractRepository repository) {
Object responseObject = null;
try {
JAXBContext jc = JAXBContext.newInstance(jaxbInterfaceClass);
Unmarshaller u = jc.createUnmarshaller();
InputStream resourceAsStream = TestProvidePensionValuationRepository.class.getClassLoader()
.getResourceAsStream(stubbedXmlFile);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
ByteArrayInputStream byteStream =
new ByteArrayInputStream(sb.toString().getBytes());
responseObject = u.unmarshal(byteStream);
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseObject;
}
My goal is not to return an Object, but the same class which is passed in as a parameter (e.g. the jaxbInterfaceClass
). I'm thinking this might be possible with Generics, but as a relative newbie to generics I'm stumped. Any help appreciated!