java - Inheritance of model class with generic and inner classes -
i have implemented inheritance hierarchy: generic
extended insurance
witch contains inner (non static) class houseinsurance
generic class extended
public abstract class generic<t extends generic> { public generic() { entityclass = ((class) ((class) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0])); } }
class extend generic , have nested class extend outer (enclosing) class insurance
public class insurance extends generic<insurance> { public class houseinsurance extends insurance { } }
unfortunately when trying create instance of inner class getting error:
classcastexception: java.lang.class cannot cast java.lang.reflect.parameterizedtype
i suppose should add inner class declaration parameter type. not know how.
generic class extended many other classes , works. first inheritance inner class, think there problem.
please help. far getting compilation error.
i not sure when create instance of houseinsurance
class class should entityclass
refer to? giving both solutions:
if entityclass
should point houseinsurance
class use code:
public generic() { type generictype = getclass().getgenericsuperclass(); if (generictype instanceof parameterizedtype) { entityclass = ((class) ((class) ((parameterizedtype) generictype).getactualtypearguments()[0])); } else { entityclass = getclass().getsuperclass(); } }
and if entityclass
should point insurance
class use instead:
public generic() { class cclass = getclass(); { type generictype = cclass.getgenericsuperclass(); if (generictype instanceof parameterizedtype) { entityclass = ((class) ((class) ((parameterizedtype) generictype).getactualtypearguments()[0])); } else { cclass = cclass.getsuperclass(); } } while (entityclass == null); }
Comments
Post a Comment