[英]Nested class cannot be cast to java.lang.reflect.ParameterizedType
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: 不幸的是,当我尝试创建TplInsuranceDoctor
对象时,遇到了运行时错误,该方法是嵌套类:
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. 我知道我应该以某种方式将嵌套类TplInsuranceDoctor
参数化类型,但是我不知道如何。 Everything I've tried fail compilation. 我尝试过的所有操作均无法编译。 Please help 请帮忙
if you call getGenericSuperclass()
on TplInsuranceDoctor
, it fails because its superclass is TplInsurance
, which is not a parameterized type. 如果在TplInsuranceDoctor
上调用getGenericSuperclass()
,它将失败,因为其超类是TplInsurance
,这不是参数化类型。 You have to go one step further to get to Generic
. 您必须进一步走才能到达Generic
。 For example you can do this : 例如,您可以执行以下操作:
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, your question would be a lot clearer if you removed all the JPA / Hibernate stuff (annotations...) which really have nothing to do with the problem. 另外,如果您删除了实际上与问题无关的所有JPA / Hibernate内容(注释...),您的问题将更加清楚。
While the accepted answer works for this exact scenario, there are numerous variations that it will fail for. 尽管可接受的答案适用于这种确切的情况,但是会有很多变体会导致失败。 You might check out TypeTools instead: 您可以改用TypeTools :
public Generic() {
entityClass = TypeResolver.resolveRawClass(Generic.class, getClass());
}
Here is my working solution. 这是我的工作解决方案。 Thanks to yannick1976 感谢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]));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.