java-如何创建已知类型的类文字:Class >
采取以下措施:
public Class> getObjectType() {
// what can I return here?
}
我可以从该方法返回满足通用性和编译性的类文字表达吗? getObjectType()无法编译,Class>也不会编译。
如果您想知道“为什么”,我正在编写Spring getObjectType()的实现,它要求我实现Class>。但是,这不是Spring的问题。
编辑:我的抱怨已经被SpringSource听到了,所以Spring 3.0.1的返回类型将getObjectType()更改为Class>,这可以避免问题。
skaffman asked 2019-11-04T20:59:10Z
10个解决方案
45 votes
您总是可以像这样投射自己想要的东西
return (Class>) new ArrayList().getClass();
要么
return (Class>) Collections.emptyList().getClass();
但是我认为那不是你所追求的。 很好,它带有警告,但并非完全“美丽”。
我刚发现这个
为什么通配符参数化类型没有类文字?
因为通配符参数化类型没有确切的运行时类型表示形式。
因此,铸造可能是唯一的方法。
Bozho answered 2019-11-04T20:59:55Z
15 votes
您永远不要使用构造Class>。这是荒谬的,应该在Java中产生警告(但不会)。 类实例始终代表原始类型,因此您可以拥有Class; 而已。 如果要用某种东西来表示诸如List这样的通用化类型,则需要像Guice使用的“超级类型令牌”:
[http://google-guice.googlecode.com/git/javadoc/com/google/inject/TypeLiteral.html]
Kevin Bourrillion answered 2019-11-04T21:00:26Z
7 votes
Class>的存在本质上是危险的。 原因如下:
// This statement generates a warning - for a reason...
Class> unsafeListClass = (Class>) (Class>) List.class;
List integerList = new ArrayList(); // Ok
integerList.add(42); // Ok
System.out.println(unsafeListClass.isInstance(integerList)); // Prints "true".
List stringList =
unsafeListClass.cast(integerList); // Succeeds, with no warning!
stringList.add("Hello, World!"); // Also succeeds with no warning
for (int x: integerList) {
// Compiles without warning, but throws ClassCastException at runtime
System.out.println(100-x);
}
finnw answered 2019-11-04T21:00:50Z
6 votes
在springframework.org上找到了此链接,该链接提供了一些见解。
例如。
List myList = new ArrayList();
return (Class>)myList.getClass();
JRL answered 2019-11-04T21:01:16Z
5 votes
您可以这样实现该方法:
public Class> getObjectType() {
return (Class>) ((Class)List.class);
}
Saeed Zarinfam answered 2019-11-04T21:01:40Z
2 votes
在SUN论坛上查看此讨论:
[http://forums.sun.com/thread.jspa?threadID=5253007]
以及引用的博客文章,其中描述了使用“超级类型令牌”的解决方法:
[http://gafter.blogspot.com/2006/12/super-type-tokens.html]
Mike Tunnicliffe answered 2019-11-04T21:02:23Z
1 votes
我不确定这是否完全可能,因为任何类文字都会被编译为Class.forName(...),并且由于这是在运行时发生的,因此没有剩余的通用信息。
perdian answered 2019-11-04T21:02:48Z
0 votes
我不确定,但是我问的以下问题可能与您有关...
Java泛型类型参数和对这些类型的操作
Yaneeve answered 2019-11-04T21:03:19Z
0 votes
那这个呢:
public class TestMain {
public static void main(String[] args) throws Exception {
Type type = TestMain.class.getMethod("dummy").getGenericReturnType();
System.out.println("type = " + type);
}
public List dummy() {return null;}
}
打印:
type = java.util.List
mazi answered 2019-11-04T21:03:45Z
0 votes
以下方法存在问题:
> public Class> getModelType() {
> return (Class>) new ArrayList().getClass();
> }
例如 如果要测试对象是否说类型
org.eclipse.emf.common.util.BasicEList
是类型
List
基于上述getModelType()方法的结果,例如:
BasicEList fromObject = ...;
if (getModelType().isAssignableFrom(fromObject.getClass())) {
transferFromModelToUi(getModelType().cast(fromObject));
}
这将导致false,但应为true,因为两个对象都实现了接口List(因为getModelType()返回的是List类型而不是ArrayList的Class对象)。
这是一种对我有用的方法(有点麻烦,但在上面的示例中导致正确的结果,可以移到静态初始化程序中):
public Class> getModelType() {
Class> arrayListClass = new ArrayList().getClass();
Class>[] interfaces = arrayListClass.getInterfaces();
int index = 0;
for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].equals(List.class)) {
index = i;
break;
}
}
return (Class>) interfaces[index];
}
Lukas Z. answered 2019-11-04T21:04:41Z