简体   繁体   English

class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized

[英]class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized

Java 11 Java 11

I need to get generic type on runtime.我需要在运行时获取泛型类型。

Here client code:这里客户端代码:

     var dataTemplateClient = new DataTemplateJdbc<Client>(dbExecutor, entitySQLMetaDataClient); 

    public class Client {
        @Id
        private Long id;
        private String name;
    }

Here code:这里代码:

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

    public class DataTemplateJdbc<T> implements DataTemplate<T> {
        private final DbExecutor dbExecutor;
        private final EntitySQLMetaData entitySQLMetaData;
    
        public DataTemplateJdbc(DbExecutor dbExecutor, EntitySQLMetaData entitySQLMetaData) {
            this.dbExecutor = dbExecutor;
            this.entitySQLMetaData = entitySQLMetaData;
            ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
            Type type = genericSuperclass.getActualTypeArguments()[0];
        }

But I get error in this line:但我在这一行得到错误:

ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();

Error:错误:

Exception in thread "main" java.lang.ClassCastException: class java.lang.Class cannot be cast to class java.lang.reflect.ParameterizedType (java.lang.Class and java.lang.reflect.ParameterizedType are in module java.base of loader 'bootstrap')
    at mypackage.DataTemplateJdbc.<init>(DataTemplateJdbc.java:26)

Basically, what you are trying to do won't work.基本上,您尝试做的事情是行不通的。 You cannot get the actual type T at runtime.您无法在运行时获得实际的T类型。

When you call getClass() on a DataTemplateJdbc , it will give you the Class object that represents the erased type.当您在DataTemplateJdbc上调用getClass()时,它将为您提供代表已擦除类型的Class object。 That is all that is available.这就是所有可用的。

The reason that the typecast fails is that a Class can never represent a parameterized type.类型转换失败的原因是Class永远不能表示参数化类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType - java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType ClassCastException:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - ClassCastException:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType “无法转换为java.lang.Class”,即:哪种类型不是类 - “Cannot be cast to java.lang.Class”, namely: which type is not a class 测试DAO时出错:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法强制转换为java.lang.Class - Error testing DAOs: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class Java:ClassCastException-无法将java.lang.Class强制转换为 - Java: ClassCastException - java.lang.Class cannot be cast to 引起:java.lang.ClassCastException:使用Generic Type时,libcore.reflect.ParameterizedTypeImpl无法强制转换为java.lang.Class - Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type 设计模式:Lazy Singleton,Generics和Inheritance:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - Design Pattern: Lazy Singleton, Generics and Inheritance: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 使用java.lang.Class的实例创建参数化类的实例 - Creating an instance of a parameterized class using an instance of java.lang.Class Spring,CXF,java.lang.Class无法转换为[Ljava.lang.Class; - Spring, CXF, java.lang.Class cannot be cast to [Ljava.lang.Class;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM