Possible Duplicate:
how to get class instance of generics type T
Template’s T
at
JAXBContext jaxbContext = JAXBContext.newInstance(T.class);
can not compile T.class , do it need reflection and how?
public void ConvertObjectToXML(String path, T bobject)
{
//Convert XML to Object
File file = new File(path);
JAXBContext jaxbContext = JAXBContext.newInstance(T.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
T customer2 = (T) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer2);
}
Due to the way Java handles generic types, this code cannot work:
You need to pass the actual generic type to the constructor (or any other method):
The classic way is to infer the generic type from an array as an array does hold its type:
This is because the actual runtime type of
Factory<T>is merelyFactory, so the runtime has no access to its generic type.