0

我正在尝试实现一个泛型类。界面如下:

public interface InterfaceClass <E extends Object, T extends APIWrapper<E>> 

该接口的摘要如下:

public abstract class MyABS <E,T extends APIWrapper<E>>
   implements InterfaceClass<E,T>
{
   private Class<T> wrapperClass;
   private Class<E> entityClass;

   public RestEndpointImpl(){
       ParameterizedType genericSuperclass =
          (ParameterizedType) getClass().getGenericInterfaces()[0];
       this.entityClass =
          (Class<E>) genericSuperclass.getActualTypeArguments()[0];
       this.wrapperClass =
          (Class<T>) genericSuperclass.getActualTypeArguments()[1];
}
...

那么我的实现是:

public class MyImpl extends MyABS<Sport,SportWrapper>

当我尝试实例化 bean 时,出现以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.windy.rest.endpoint.MyImpl' defined in file [/Users/ddelizia/Documents/My-Projects/windy/repository/blc-ddelizia/rest/target/ddelizia/WEB-INF/classes/com/ddelizia/rest/endpoint/MyImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ddelizia.rest.endpoint.MyImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
[artifact:mvn]  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
[artifact:mvn]  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
[artifact:mvn]  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
[artifact:mvn]  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
[artifact:mvn]  at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
[artifact:mvn]  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
[artifact:mvn]  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:766)
[artifact:mvn]  at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:406)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:758)
[artifact:mvn]  at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:242)
[artifact:mvn]  at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1233)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:701)
[artifact:mvn]  at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:475)
[artifact:mvn]  at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90)
[artifact:mvn]  at org.eclipse.jetty.server.Server.doStart(Server.java:272)
[artifact:mvn]  at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:511)
[artifact:mvn]  at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:364)
[artifact:mvn]  at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:516)
[artifact:mvn]  at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
[artifact:mvn]  at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
[artifact:mvn]  at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
[artifact:mvn]  at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
[artifact:mvn]  at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
[artifact:mvn]  at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
[artifact:mvn]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[artifact:mvn]  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[artifact:mvn]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[artifact:mvn]  at java.lang.reflect.Method.invoke(Method.java:597)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[artifact:mvn]  at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
[artifact:mvn] Caused by: 
[artifact:mvn] org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ddelizia.rest.endpoint.MyImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
[artifact:mvn]  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
[artifact:mvn]  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
[artifact:mvn]  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
[artifact:mvn]  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
[artifact:mvn]  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
[artifact:mvn]  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
[artifact:mvn]  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
[artifact:mvn]  at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
[artifact:mvn]  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
[artifact:mvn]  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:766)
[artifact:mvn]  at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:406)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:758)
[artifact:mvn]  at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:242)
[artifact:mvn]  at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1233)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:701)
[artifact:mvn]  at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:475)
[artifact:mvn]  at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
[artifact:mvn]  at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90)
[artifact:mvn]  at org.eclipse.jetty.server.Server.doStart(Server.java:272)
[artifact:mvn]  at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
[artifact:mvn]  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
[artifact:mvn]  at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:511)
[artifact:mvn]  at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:364)
[artifact:mvn]  at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:516)
[artifact:mvn]  at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
[artifact:mvn]  at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
[artifact:mvn]  at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
[artifact:mvn]  at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
[artifact:mvn]  at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
[artifact:mvn]  at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
[artifact:mvn]  at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
[artifact:mvn]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[artifact:mvn]  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[artifact:mvn]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[artifact:mvn]  at java.lang.reflect.Method.invoke(Method.java:597)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
[artifact:mvn]  at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[artifact:mvn]  at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
[artifact:mvn] Caused by: 
[artifact:mvn] java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

关于我做错了什么的任何想法?

4

2 回答 2

4

乍一看,您似乎想调用getGenericSuperclass()而不是getGenericInterfaces()

ParameterizedType genericSuperclass =
   (ParameterizedType) getClass().getGenericSuperclass();
于 2013-02-27T20:09:20.233 回答
0

这很简单。你投错了班级。请看这里: http ://docs.oracle.com/javase/6/docs/api/java/lang/reflect/ParameterizedType.html

参数化类型不是类。

于 2013-02-27T20:10:49.500 回答