Error testing DAOs: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class

雨燕双飞 提交于 2019-12-05 17:29:52

The expression

getClass().getGenericSuperclass()).getActualTypeArguments()[0]

refers to value substituted to first type parameter of AbstractHibernateDAO (which in the AbstractHibernateDAO.java is denoted by T). The type substituted by subclass NominationHibernateDAO is still not concrete, it is T extends Nomination, which is definitely not a java.lang.Class instance, but a Type instance (TypeVariableImpl is an implementation of a Type).

If the NominationHibernateDAO was declared with something like that:

class NominationHibernateDAO extends AbstractHibernateDAO<Nomination, Integer> { ...

then the AbstractHibernateDAO constructor magic would work. Alternatively, you can (or you even should?) instantiate a subclass of NominationHibernateDAO declared like this:

class ConcreteNominationHibernateDAO
        extends NominationHibernateDAO<ConcreteNominationSubclass> { ...

or

new NominationHibernateDAO<ConcreteNominationSubclass>() {}

This, again, would not trigger the problem.

Are you sure the NominationHibernateDAO class is meant to be used as a Spring bean?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!