Can generics avoid me passing back a, uh, generic 'Object'?
I've got the following JAXB unmarshalling code which I'm trying to generalize. Here's what it looks like:
开发者_StackOverflow社区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!
This isn't really a JAXB issue at all, just some tweaking of the generics required:
private <T> T getResponseObject(String stubbedXmlFile,
Class<T> jaxbInterfaceClass,
AbstractRepository repository) {
T responseObject = null;
// code as before
responseObject = (T) u.unmarshal(byteStream);
return responseObject;
}
This will likely raise a compiler warning, but that's not uncommon with code that bridges generics code with non-generics code like JAXB.
You can then call this with:
MyType x = getResponseObject(file, MyType.class, repo);
You can make @skaffman's code type-safe with:
responseObject = jaxbInterfaceClass.cast(u.unmarshal(byteStream));
note, however, that you should probably deal with JAXBElement...
精彩评论