generics - Nested class cannot be cast to java.lang.reflect.ParameterizedType -


i trying implement model structure this:

generic (abstract) tplinsurance (extends generic) tplinsurancedoctor (nested class in tplinsurance extends tplinsurance) 

unfortunatelly getting runtime error when trying create object of tplinsurancedoctor, wich nested class:

caused by: java.lang.classcastexception: java.lang.class cannot cast java.lang.reflect.parameterizedtype 

that errors points generic contructor:

public generic() {     entityclass = ((class) ((class) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0])); } 

here 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 dentysty";     } 

here way create object:

tplinsurancedoctor tdoctor = new tplinsurance().new tplinsurancedoctor(); 

i understand should somehow parametrise type of nested class tplinsurancedoctor, dont know how. i've tried fail compilation. please help

if call getgenericsuperclass() on tplinsurancedoctor, fails because superclass tplinsurance, not parameterized type. have go 1 step further generic. example can :

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])); } 

also, question lot clearer if removed jpa / hibernate stuff (annotations...) have nothing problem.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -