Edgar
Edgar

Reputation: 1120

ClassCastException while trying to get the generic type parameter

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.

Upvotes: 2

Views: 445

Answers (3)

Chetan Kinger
Chetan Kinger

Reputation: 15212

Do not know where is problem, maybe someaone can help me

The error message is self explanatory. The following statement returns a Class :

(getClass().getGenericSuperclass())

And you are trying to cast it to a ParameterizedType

((ParameterizedType) (getClass().getGenericSuperclass()))

Class and ParameterizedType are siblings. A brother is not a sister so trying to cast them into one another would be cruel.

That being said, a quick solution to your problem would be to ask the client code to pass the Class type to your XmlUtils class.

public class XMLUtils<MODEL extends AbstractModel, WRAPPER extends WraperInterface<MODEL>> {

private WRAPPER wrapperInstance;

public XMLUtils(Class<WRAPPER> wrapperInstance) throws InstantiationException, IllegalAccessException {
    this.wrapperInstance = wrapperInstance    
 }
 //more code follows
}

Upvotes: 2

Brett Walker
Brett Walker

Reputation: 3576

You probably need to use TypeVariable instead of ParameterizedType.

Have you checked the type of the object returned from getClass().getGenericSuperclass()? i.e.

Class<?> c = getClass().getGenericSuperclass();

if (c instanceof ParameterizedType) {
  System.out.println("It is an instanceof ParameterizedType");
}
else if (c instanceof TypeVariable) {
  System.out.println("It is an instanceof TypeVariable");
}
else {
  System.out.println("It is something else");
}

Upvotes: 0

Robert
Robert

Reputation: 42575

What do you expect as result?

To simplify your question one cans say you have the following class:

class XMLUtils extends java.lang.Object {
  public XMLUtils() {
      getClass().getGenericSuperclass()// == java.lang.Object
  }
}

Now you are trying to cast java.lang.Object to ParameterizedType but that will only work in case the class is a generic class. Therefore it fails...

Upvotes: 0

Related Questions