【发布时间】:2014-01-01 21:32:23
【问题描述】:
具体来说 - 我什至不知道这是否可能,但可能有一些东西,我错过了。 我需要从 String 中获取类类型并将其用作泛型类型:
//Don't mind this
String columnName = n.getAttributes().getNamedItem("name").getTextContent();
//That would be canonical class name
String columnType = n.getAttributes().getNamedItem("type").getTextContent();
Column<**Type from columnName**> col = new Column<>(columnName);
这样的事情可能吗?
【问题讨论】:
-
不,不是。泛型是一个编译时概念。它们在运行时不存在。
-
可以从字符串名称中获取
Class,但这对您没有帮助。泛型参数是 compile-time 的东西;您拥有只有在运行时才知道的信息。 -
您可能需要考虑为 columnName 和 columnType 编写单独的参数化类。