I want to get instance of generic using reflection:
        this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
but I get exception:
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
Do not know where is problem, maybe someaone can help me? There is full class code:
public class XMLUtils<MODEL extends AbstractModel, WRAPPER extends WraperInterface<MODEL>> {
private WRAPPER wrapperInstance;
@SuppressWarnings("unchecked")
public XMLUtils() throws InstantiationException, IllegalAccessException {
    this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
}
@SuppressWarnings("unchecked")
public List<MODEL> loadDataFromXML(String fileName) {
    try {
        File pajamuFile = new File(
                UserDataLoader.INSTANCE.getCurrentUserFolder() + fileName);
        JAXBContext context = JAXBContext.newInstance(wrapperInstance.getClass());
        Unmarshaller um = context.createUnmarshaller();
        WRAPPER wrapper = (WRAPPER) um.unmarshal(pajamuFile);
        return wrapper.getDataList();
    } catch (JAXBException e) {
        e.printStackTrace();
        return new ArrayList<MODEL>();
    }
}
public void saveDataToXML(List<MODEL> dataList, String fileName) {
    try {
        File pajamuFile = new File(
                UserDataLoader.INSTANCE.getCurrentUserFolder() + fileName);
        JAXBContext context = JAXBContext.newInstance(wrapperInstance.getClass());
        Marshaller m = context.createMarshaller();
        WRAPPER wrapper = wrapperInstance;
        wrapper.setDataList(dataList);
        m.marshal(wrapper, pajamuFile);
    } catch (JAXBException  e) {
        e.printStackTrace();
    }
}
}
In google I find most such like situation but with spring, here I am not using spring, so maybe there is any clear solution for that what I want to do.
 
Aucun commentaire:
Enregistrer un commentaire