JAXBElement<T> make the declared type T as a parameter to work with any class
質問
I got this code
private Object fooMethod(Node node, Class classOut)
JAXBElement<MessageAcknowledgementType> root = unmarshallerjaxObject.unmarshal(node, MessageAcknowledgementType.class);
Since I am going to call the fooMethod with different class names, I want to specify into the JAXBElement the name of the class from the variable classOut, I have tried this but it doesn't work:
JAXBElement <classOut.getSimpleName()> root = unmarshallerjaxObject.unmarshal(node, classOut.getSimpleName()); //It doesn't work
Any idea about how to specify the class name into the JAXBElement?. Thank you!.
解決
The simplest thing is:
private Object fooMethod(Node node, Class<?> classOut) {
JAXBElement<?> root = unmarshallerjaxObject.unmarshal(node, classOut);
}
他のヒント
How about this:
Object obj = fooMethod(node, classout);
Define another method which will cast it to the appropriate type for you:
private <T> JAXBElement<T> getCast(Class<T> clazz, Object obj){
return (JAXBElement<T>)obj;
}
JAXBElement<Member> jaxbElement = getCast(SomeClass.class, obj);
所属していません StackOverflow