0

I have an abstract class which generics and tried to extend it passing through the generics.
But it seems that it isn't possible to get the generic type anymore.

The root class

public abstract class AbstractDataModel<T extends AbstractEntity> extends DataModel<T> {

    private final Class<T> rootType;

    protected AbstractDataModel() {
        Type genericSuperclass = getClass().getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType)genericSuperclass;
        Type type = parameterizedType.getActualTypeArguments()[0];
        rootType = (Class<T>)type;
    }

The specialization

public class OwnerDataModel<T extends AbstractEntity> extends AbstractDataModel<T> {

I get an exception when casting type to Class<T>. I got

Cannot cast 'sun.reflect.generics.reflectiveObjects.TypeVariableImpl' to 'java.lang.Class'

Normally I am using the AbstractDataModel directly

dataModel = new AbstractDataModel<Entity>() {

And I want to use my OwnerDataModel like the following

dataModel = new OwnerDataModel<Entity>();

What did I miss?

CSchulz
  • 10,102
  • 9
  • 54
  • 107

1 Answers1

0

First, you don't have to write this :

public class OwnerDataModel<T extends AbstractEntity> extends AbstractDataModel<T> { ...

but this : public class OwnerDataModel extends AbstractDataModel<T> { ...

because, is included in the first by default by AbstractDataModel<T>.

Second, You can't instantiate AbstractDataModel but the class OwnerDataModel .

bilelovitch
  • 1,455
  • 14
  • 22