异常:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType解决方案

异常:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType解决方案

1.先来看看异常日志 (java.lang.Class不能被转换为java.lang.reflect.ParameterizedType)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘baseTestImpl’ defined in file [D:\workspace\jc-demo\gateway\target\classes\com\jc\gateway\test\base\BaseTestImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.jc.gateway.test.base.BaseTestImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1303) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1197) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at com.jc.gateway.GatewayApplication.main(GatewayApplication.java:17) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.jc.gateway.test.base.BaseTestImpl]: 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:184) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1295) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
… 17 common frames omitted
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.jc.gateway.test.base.BaseTestImpl.(BaseTestImpl.java:24) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_301]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_301]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_301]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_301]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
… 19 common frames omitted

2.很明显错误指向BaseTestImpl.java:24,打断点检查


我用的是springboot+springcloud框架,启动main方法时BaseTestImpl构造器中反射类型转型失败异常,导致无法创建BaseTestImpl bean

3.解决方案如下(加入if条件构造器运行时)

    Class<V> vClass;

    public BaseTestImpl() {
        //获取泛型的class类型
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        if(genericSuperclass instanceof ParameterizedType){
            Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
            this.vClass = (Class<V>) actualTypeArguments[0];
        }
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 这个错误是因为在Java中,一个类不能被强制转换为ParameterizedType类型。通常是因为在使用反射时,尝试将一个Class对象转换为ParameterizedType对象,但是这个Class对象实际上不是一个ParameterizedType类型。要解决这个问题,需要检查代码中的反射使用,确保正确地使用了ParameterizedType类型。 ### 回答2: java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType。 在Java中,java.lang.Class类表示运行时类的信息和属性,而java.lang.reflect.ParameterizedType接口表示带有参数的类型(泛型类型)。 这个错误是因为在尝试将Class对象强制转换为ParameterizedType对象时,类型转换发生了错误。 要解决这个问题,我们需要理解java.lang.reflect.ParameterizedType的使用情况。 ParameterizedType接口是Type接口的子接口,可以用于获取带有参数的类型信息,例如泛型类或泛型接口的类型参数信息。 如果我们想要获得一个类的泛型参数类型信息,我们可以使用java.lang.Class类中的getGenericSuperclass()方法或getGenericInterfaces()方法。 但是要注意,这些方法返回的类型是java.lang.reflect.Type,而不是java.lang.reflect.ParameterizedType。 如果我们想要将Type对象转换为ParameterizedType对象,我们需要进行类型检查和类型转换。 示例代码: import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class MyClass<T> { public MyClass() { Type type = getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; // 在这里可以使用ParameterizedType对象来获取泛型参数类型信息 } } } 总结来说,java.lang.Class不能直接强制转换为java.lang.reflect.ParameterizedType,我们需要根据具体的使用情况进行类型检查和转换才能正确地获取泛型参数类型信息。 ### 回答3: java.lang.Class不能被转换为java.lang.reflect.ParameterizedType的原因是两者表示的是不同的类型信息。java.lang.Class用于表示类的信息,而java.lang.reflect.ParameterizedType用于表示泛型类型的信息。 当使用强制类型转换将一个对象转换为java.lang.reflect.ParameterizedType时,如果该对象实际上是java.lang.Class类型的对象,就会抛出ClassCastException。这是因为java.lang.Classjava.lang.reflect.ParameterizedType是不兼容的类型。 如果需要获取一个类中的泛型类型信息,应该使用java.lang.Class对象的getGenericSuperclass()方法来获取其父类的泛型类型信息,再通过强制类型转换获取到java.lang.reflect.ParameterizedType对象。 例如: ``` class MyGenericClass<T> { } class MyClass extends MyGenericClass<String> { } public static void main(String[] args) { MyClass myClass = new MyClass(); ParameterizedType parameterizedType = (ParameterizedType) myClass.getClass().getGenericSuperclass(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); Type actualTypeArgument = actualTypeArguments[0]; String genericType = actualTypeArgument.getTypeName(); System.out.println(genericType); } ``` 如果在强制类型转换时抛出了java.lang.ClassCastException,可以检查代码中的类型转换部分,并确保正确使用泛型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值