CDI 注入:java.lang.Class 无法转换为 java.lang.reflect.ParameterizedType

CDI Inject: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

正在尝试创建 DAO 层但每次都出现以下异常

javax.ejb.EJBException: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

我看过相关文章,发现我的结构按照定义是正确的,但我不确定为什么会出现此异常?

接口:

public interface GenericDao<T, PK> {
       long countAll(/* Map<String, Object> params */);

       void createAll(Collection<T> ts);

       T create(T t);

摘要Class:

public abstract class GenericDaoImpl<T, PK> implements GenericDao<T, PK> {

    @PersistenceContext(unitName = "IntegratorMasterdataDS")
    protected EntityManager em;

    private final Class<T> type;

    public GenericDaoImpl() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) t;
        type = (Class) pt.getActualTypeArguments()[0];
    }

扩展接口:

   public interface CustomerAgreementDao extends GenericDao<CustomerAgreement, Long>{
    CustomerAgreement create(CustomerAgreement person);

扩展实施:

     @Named("customerAgreementDao")
@ApplicationScoped
public class CustomerAgreementDaoImpl extends GenericDaoImpl<CustomerAgreement, Long> implements CustomerAgreementDao {

    @Override
    public CustomerAgreement create(CustomerAgreement customerAgreement) {
      return (CustomerAgreement) create((CustomerAgreement) customerAgreement);
    }

这是我从 Spring 代码复制而来的,它工作正常,没有任何问题。任何人都可以建议这是什么问题。我需要在某处添加另一个注释吗?

异常详情:

2019-11-29 11:21:00,523 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 104) MSC000001: Failed to start service jboss.deployment.unit."Snow-1.1-SNAPSHOT.war".component.SnowPoller.START: org.jboss.msc.service.StartException in service jboss.deployment.unit."Snow-1.1-SNAPSHOT.war".component.SnowPoller.START: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
        at org.jboss.as.ee.component.ComponentStartService.run(ComponentStartService.java:57)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
        at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
        at java.lang.Thread.run(Thread.java:745)
        at org.jboss.threads.JBossThread.run(JBossThread.java:485)
Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:134)
        at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:88)
        at org.jboss.as.ejb3.component.singleton.SingletonComponent.getComponentInstance(SingletonComponent.java:127)
        at org.jboss.as.ejb3.component.singleton.SingletonComponent.start(SingletonComponent.java:141)
        at org.jboss.as.ee.component.ComponentStartService.run(ComponentStartService.java:54)
        ... 8 more
Caused by: javax.ejb.EJBException: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:246)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:388)
        at org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor.processInvocation(LifecycleCMTTxInterceptor.java:68)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.weld.injection.WeldInjectionContextInterceptor.processInvocation(WeldInjectionContextInterceptor.java:43)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:60)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.ejb3.component.singleton.StartupCountDownInterceptor.processInvocation(StartupCountDownInterceptor.java:25)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53)
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
        ... 13 more
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
        at com.evry.integrator.snow.model.dao.impl.GenericDaoImpl.<init>(GenericDaoImpl.java:22)
        at com.evry.integrator.snow.model.dao.impl.CustomerAgreementDaoImpl.<init>(CustomerAgreementDaoImpl.java:13)
        at com.evry.integrator.snow.model.dao.impl.CustomerAgreementDaoImpl$Proxy$_$$_WeldClientProxy.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.jboss.weld.bean.proxy.DefaultProxyInstantiator.newInstance(DefaultProxyInstantiator.java:43)
        at org.jboss.weld.bean.proxy.ProxyFactory.run(ProxyFactory.java:339)
        at org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:323)
        at org.jboss.weld.bean.proxy.ClientProxyFactory.create(ClientProxyFactory.java:83)
        at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:205)
        at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:195)
        at org.jboss.weld.bean.proxy.ClientProxyProvider.access0(ClientProxyProvider.java:44)
        at org.jboss.weld.bean.proxy.ClientProxyProvider$CreateClientProxy.apply(ClientProxyProvider.java:52)
        at org.jboss.weld.bean.proxy.ClientProxyProvider$CreateClientProxy.apply(ClientProxyProvider.java:48)
        at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.lambda$null[=17=](ReentrantMapBackedComputingCache.java:55)
        at org.jboss.weld.util.LazyValueHolder.computeValue(LazyValueHolder.java:32)
        at org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:46)
        at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.getValue(ReentrantMapBackedComputingCache.java:72)
        at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.getCastValue(ReentrantMapBackedComputingCache.java:78)
        at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:229)
        at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:688)
        at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:794)
        at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
        at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:336)
        at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:347)
        at org.jboss.weld.injection.producer.DefaultInjector.proceed(DefaultInjector.java:71)
        at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
        at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:73)
        at org.jboss.weld.module.ejb.DynamicInjectionPointInjector.inject(DynamicInjectionPointInjector.java:61)
        at org.jboss.weld.module.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:138)
        at org.jboss.as.weld.injection.WeldInjectionContext.inject(WeldInjectionContext.java:39)
        at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:51)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.ee.component.AroundConstructInterceptorFactory.processInvocation(AroundConstructInterceptorFactory.java:28)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.java:56)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.weld.interceptors.Jsr299BindingsCreateInterceptor.processInvocation(Jsr299BindingsCreateInterceptor.java:111)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:237)
        ... 28 more

我刚刚在 GenericDaoImpl 上添加了@SuppressWarnings({"unchecked", "rawtypes"}) 错误消失了。

@SuppressWarnings({"unchecked", "rawtypes"})
public GenericDaoImpl() {

    Type t = getClass().getGenericSuperclass();
    ParameterizedType pt = (ParameterizedType) t;
    type = (Class<T>) pt.getActualTypeArguments()[0];
}

但不知道为什么?我不确定这是否是这个问题的答案。 所以这只是一般分享。

注意:当我将 Class<T> 恢复为 Class 时它甚至可以正常工作