在运行时获取类的通用类型不起作用

妮基·米什拉(Nikhil Mishra)

我有两个类和一个接口

public class ExcelReader<T> extends Reader<T> implements RowColumnReader<T> {

    // private final TypeToken<T> typeToken = new TypeToken<T>(getClass()) {};
    // private final Type type = typeToken.getType(); 
    /* public Type getType() {
        return type;
        }*/
    // Useing Guava getting System.out.println(reader.getType()) T as ouput

        @Override
        public List<T> readFile(String file) {
          //my code
        }
    }
public abstract class Reader<T> {
    protected Class<T> clazz;

    /*public Reader() {
        clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }*/
    // Constructor is commented beacuse getting Exception java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
}

public interface RowColumnReader<T> extends HeaderFields<T>{

    public List<T> readFile(String file);
}

public interface HeaderFields<T> {

    default List<String> getHeader(Class<T> clazz) throws HeaderNotDefiendException {
        //my code
        return headerList;
    }

}

我尝试了所有可能的方法来获取泛型类型的类。在运行时遵循了Get类的通用类型,甚至尝试了该页面中给出的所有解决方案。但仍然没有任何运气。

即使有高瓦

https://stackoverflow.com/a/19775924/2290078获取的输出getType()不是实际的通用类

我的测试代码是

Reader<Test> reader = new ExcelReader<>();
//System.out.println(reader.getType());  ---> output is T 
List<Test> list = reader.readFile("Sample.xlsx");
System.out.println(list);
大卫xxx

我尝试了所有可能的方法来获取泛型类型的类。

您在这里发现的这种方式不适用于检索泛型:

public Reader() {
    clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

因为它静态检索在的直接超类中定义的参数化类型Reader,在这种情况下为Object

在子类构造函数中移动此代码也不起作用:

public ExcelReader() {
    clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

因为它静态检索类中定义的参数化类型Reader,即T由于T没有任何限制,您仍将获得Object该类作为返回的类。

哪种解决方案适合您的情况?

泛型在编译后被擦除。因此请注意,在运行时使用的参数化类型信息Test如下:

Reader<Test> reader = new ExcelReader<>();

您必须Class在实际代码中显式“传递”表示泛型的代码。
为此,请在构造函数中添加一个Class表示通用类型参数,并将其存储在字段中。现在,您可以在运行时引用类型。

public class ExcelReader<T> extends Reader<T> implements RowColumnReader<T> {

   private Class<T> genericType;

   public ExcelReader(Class<T> genericType){
       this.genericType = genericType;
   }
   /*...*/
}

现在您可以ExcelReader用这种方式实例化

ExcelReader<Test> excelReader = new ExcelReader<>(Test.class);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在运行时按名称获取通用类型

具有记录类型的 Typeinfo 在运行时不起作用

wpf 控制模板在运行时不起作用

动画正在运行时,OnClick不起作用

Sub在运行时起作用,但在调用时却不起作用?“范围类的选择方法失败”

EnumSet类,在运行时获取元素类型

使用TypeBuilder获取在运行时创建的类的类型

在运行时获取模板类型

在运行时获取输入类型

在运行时确定通用方法参数的类型

在运行时将对象转换为通用类型

SwitchCompat setTextOn() 和 setTextOff() 在运行时不起作用

C#复制到剪贴板在运行时不起作用

在运行时为Chromium提供API密钥不起作用

在运行时创建dll文件在C#中不起作用

android:layout_alignParentBottom在运行时不起作用

在运行时更新表名不起作用-laravel Eloquent ORM

在运行时渲染DOM在Dojox中不起作用

WPF在运行时窗口边距不起作用

文本 HorzAlign 在运行时创建的标签上不起作用

当地图正在运行时,React中的过滤器方法不起作用

DotVVM在运行时从Panel组件更改CssStyle属性不起作用

将日期插入表变量在SSMS中不起作用,但在运行时起作用

在运行时从指向基类的指针获取对象的类型

Aceess VBA-从网站获取数据的子例程在调试模式下有效,但在运行时不起作用

在运行时选择类成员的类型

如何在运行时获取对象类型?

是否可以在运行时获取类型参数?

Android在运行时获取EditText输入类型