0

I am trying to implement model structure like this:

Generic (abstract)
TplInsurance (extends Generic)
TplInsuranceDoctor (nested class in TplInsurance extends TplInsurance)

Unfortunatelly I am getting runtime error when I am trying to create object of TplInsuranceDoctor, wich is nested class:

Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

That errors points to Generic contructor:

public Generic() {
    entityClass = ((Class) ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
}

Here are model classes:

public abstract class Generic<T extends Generic> {
    protected Class<T> entityClass;
    public Generic() {
        entityClass = ((Class) ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
    }
    public Long id;
}

public class TplInsurance extends GenericDictionary<TplInsurance> {

    public class TplInsuranceDoctor extends TplInsurance {
        public final String color = "success";
        public final String title = "lekarza i dentysty";
    }

Here is The way I create object:

TplInsuranceDoctor tDoctor = new TplInsurance().new TplInsuranceDoctor();

I understand That I should somehow parametrise type of nested class TplInsuranceDoctor, but I dont know how. Everything I've tried fail compilation. Please help

4

3 回答 3

3

如果调用getGenericSuperclass()on TplInsuranceDoctor,它会失败,因为它的超类是TplInsurance,它不是参数化类型。你必须更进一步才能到达Generic。例如,您可以这样做:

public Generic() {
    Class class1 = getClass();
    Type genericSuperclass = null;
    for(;;) {
        genericSuperclass = class1.getGenericSuperclass();
        if(genericSuperclass instanceof ParameterizedType)
            break;
        class1 = class1.getSuperclass();
    }
    ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
    entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
}

此外,如果您删除了所有与问题无关的 JPA / Hibernate 内容(注释......),您的问题会更加清晰。

于 2015-05-15T11:44:26.530 回答
2

虽然公认的答案适用于这种确切的场景,但它会因多种变化而失败。您可以改为查看TypeTools

public Generic() {
    entityClass = TypeResolver.resolveRawClass(Generic.class, getClass());
}
于 2015-05-27T04:32:57.693 回答
0

这是我的工作解决方案。感谢yannick1976

@Transient protected Class<T> entityClass;
public Generic() {
    // for standard class. Dosen't take care of inner/nested classes
    //entityClass = ((Class) ((Class) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[0]));
    // works for nested/inner classes
    Class obtainedClass = getClass();
    Type genericSuperclass = null;
    for(;;) {
        genericSuperclass = obtainedClass.getGenericSuperclass();
        if(genericSuperclass instanceof ParameterizedType) {
            break;
        }
        obtainedClass = obtainedClass.getSuperclass();
    }
    ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
    entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
}
于 2015-05-18T11:12:46.580 回答