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

来源:互联网 发布:简述算法的复杂度分析 编辑:程序博客网 时间:2024/05/01 04:56

当BaseDao不加abstract修饰的时候,this.getClass().getGenericSuperclass();报类型强转错误。

public abstract class BaseDaoImpl<T> implements BaseDao<T>{    //确定运行时使用哪个子类对象    Class<T> clazz = null;    public BaseDaoImpl(){        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();        clazz = (Class<T>) pt.getActualTypeArguments()[0];    }

java.lang.ExceptionInInitializerError
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 org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:178)
at org.junit.runners.BlockJUnit4ClassRunner1.runReflectiveCall(BlockJUnit4ClassRunner.java:227)atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)atorg.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:224)atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)atorg.junit.runners.ParentRunner3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner1.schedule(ParentRunner.java:60)atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)atorg.junit.runners.ParentRunner.access000(ParentRunner.java:50)
at org.junit.runners.ParentRunner2.evaluate(ParentRunner.java:222)atorg.junit.runners.ParentRunner.run(ParentRunner.java:292)atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Causedby:org.springframework.beans.factory.BeanCreationException:ErrorcreatingbeanwithnamebaseDaodefinedinfile[C:\Workspaces\MyEclipseProfessional2014\gyl1\hytgyl\target\classes\com\hyt\gyl\dao\core\impl\BaseDaoImpl.class]:Instantiationofbeanfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Couldnotinstantiatebeanclass[com.hyt.gyl.dao.core.impl.BaseDaoImpl]:Constructorthrewexception;nestedexceptionisjava.lang.ClassCastException:java.lang.Classcannotbecasttojava.lang.reflect.ParameterizedTypeatorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1013)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)atorg.springframework.beans.factory.support.AbstractBeanFactory1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.hyt.gyl.test.util.SpringUtil.(SpringUtil.java:9)
… 22 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.hyt.gyl.dao.core.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006)
… 35 more
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.hyt.gyl.dao.core.impl.BaseDaoImpl.(BaseDaoImpl.java:34)
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 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
… 37 more

0 0