gpt4 book ai didi

java - 我可以在通用类型上获取 Clazz.class 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:25:05 28 4
gpt4 key购买 nike

我正在尝试调用 EasyMock.isA(Class<T>)List<MyType> 上.有没有办法在没有警告的情况下做到这一点?

我试过以下方法:

isA(List<MyType>.class);  // doesn't compile
isA(List.<MyType>class); // syntax error on tokens (MyType), misplaced construct
isA(List.class); // This gives a warning: Type safety: The expression of type List needs unchecked conversion to conform to List<MyType>

编辑:

Jakub HR 给出了正确答案。然而,对于我需要 EasyMock 的特殊情况,我可以简单地使用

EasyMock.<List<MyType>>anyObject()

最佳答案

来自 Effective Java:

There are two minor exceptions to the rule that you should not use raw types in new code, both of which stem from the fact that generic type information is erased at runtime. You must use raw types in class literals. The specification does not permit the use of parameterized types (though it does permit array types and primitive types). In other words, List.class, String[].class, and int.class are all legal, but List<String>.class and List<?>.class are not.

关于java - 我可以在通用类型上获取 Clazz.class 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782240/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com