java 从泛型获取类不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14789226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:24:08  来源:igfitidea点击:

Getting class from generic not working

javagenerics

提问by Rayden78

I'm really confused right now. I'm using following code in another project without any problems:

我现在真的很困惑。我在另一个项目中使用以下代码没有任何问题:

public class ReadOnlyTable<T extends Model> implements ReadOnly<T> {
protected at.viswars.database.framework.core.DatabaseTable datasource;
private boolean debug = true;

protected ReadOnlyTable() {
    initTable();
}

protected void reloadDataSource() {
    initTable();
}

private void initTable() {
    boolean readOnlyClass = false;
    if (getClass().getSuperclass().equals(ReadOnlyTable.class)) readOnlyClass = true;

    Class<T> dataClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

The last line will run without problems. Now i made a second project and as i had problems with reading out the Class i tried to do the most simple case possible:

最后一行将毫无问题地运行。现在我做了第二个项目,因为我在读取 Class 时遇到了问题,所以我尝试做最简单的案例:

public class GenericObject<T> implements IGenericObject<T> {
public GenericObject() {
    init();
}

private void init() {
    Class<T> clazz = (Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

}

}

This class is instanciated here:

这个类在这里实例化:

        GenericObject<String> go = new GenericObject<String>();

In my second example i will always receive following error message:

在我的第二个示例中,我将始终收到以下错误消息:

Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

线程“main”中的异常 java.lang.ClassCastException:java.lang.Class 不能转换为 java.lang.reflect.ParameterizedType

What i am missing?? This drives me crazy. Any help is appreciated!

我缺少什么??这让我发疯。任何帮助表示赞赏!

回答by user000001

You are calling getGenericSuperclass(), but your class does not have a genericsuperclass. Its superclass is Object, which cannot be cast to ParameterizedTypeas the error says.

您正在调用getGenericSuperclass(),但您的类没有通用超类。它的超类是Object,不能ParameterizedType像错误所说的那样强制转换。

You probably want to use getGenericInterfaces()[0], since your class only implements an interface.

您可能想要使用getGenericInterfaces()[0],因为您的类只实现了一个接口。

More info in here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getGenericInterfaces()

更多信息在这里:http: //docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getGenericInterfaces()

回答by newacct

It is impossible to retrieve Stringfrom go. The type parameter of an object instantiation expression (new GenericObject<String>()) is not present anywhere at runtime. This is type erasure.

这是不可能恢复Stringgo。对象实例化表达式 ( new GenericObject<String>())的类型参数在运行时不存在任何地方。这是类型擦除。

The technique you are trying to use concerns when a classor interfaceextends or implements another class or interface with a specific type argument. e.g. class Something extends GenericObject<String>. Then it is possible to get this declaration informationfrom the class object. This is completely unrelated to what you are doing.

该技术您尝试当使用关注接口与特定类型的参数扩展或实现其他类或接口。例如class Something extends GenericObject<String>。那么就可以从类对象中得到这个声明信息。这与您正在做的事情完全无关。