java:如何修复未经检查的强制转换警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4388054/
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
java: How to fix the Unchecked cast warning
提问by Brad
I've got the following code:
我有以下代码:
private HashMap<Class<?>, HashMap<Entity, ? extends Component>> m_componentStores;
public <T extends Component> T getComponent(Entity e, Class<T> exampleClass)
{
HashMap<Entity, ? extends Component> store = m_componentStores.get(exampleClass);
T result = (T)store.get(e);
if (result == null)
{
throw new IllegalArgumentException( "GET FAIL: "+e+" does not possess Component of class\nmissing: "+exampleClass );
}
return result;
}
When I compile, it shows that T result = (T)store.get(e)
has an unchecked cast.
当我编译时,它显示T result = (T)store.get(e)
有一个未经检查的强制转换。
Type safety: Unchecked cast from capture#2-of ? extends Component to T
What am I missing to prevent this warning from appearing?
我缺少什么来防止出现此警告?
采纳答案by Tom Hawtin - tackline
Class.cast
is what you want. Well, you might consider not using reflection.
Class.cast
是你想要的。好吧,您可能会考虑不使用反射。
Change the line:
更改行:
T result = (T)store.get(e);
to:
到:
T result = exampleClass.cast(store.get(e));
回答by Ralph
Write @SuppressWarnings("unchecked")
above the Cast statement:
@SuppressWarnings("unchecked")
在 Cast 语句上方写上:
@SuppressWarnings("unchecked")
T result = (T)store.get(e);
And add a explanatory statement why it is safe to ignore the warning.
并添加一个解释性说明为什么忽略警告是安全的。
回答by rustyx
extends
in generics doesn't really work that way. T
!= ? extends Component
even though T extends Component
. What you have is in fact a wildcard capture, it has a different purpose.
extends
在泛型中并不是这样工作的。T
!=? extends Component
即使T extends Component
. 您所拥有的实际上是通配符捕获,它具有不同的目的。
And yes your solution is not type-safe - there is no relation between the two ?
marks in:
是的,您的解决方案不是类型安全的 - 两个?
标记之间没有关系:
private HashMap<Class<?>, HashMap<Entity, ? extends Component>> m_componentStores;
So it becomes legal to put an instance of some subclass of Component
in this structure using some other class (not even a subclass of Component
) as the key.
因此,Component
使用其他类(甚至不是 的子类Component
)作为键将某个 的子类的实例放入该结构中是合法的。
Remember that generic types are resolved at compile timeonly, so at run timem_componentStores
has no way of knowing what exact type of the value you have in there, other than that it extends
Component
.
请记住,泛型类型仅在编译时解析,因此在运行时m_componentStores
无法知道您在那里拥有的值的确切类型,除了 it extends
Component
。
So the type you get from store.get(e)
is ... Component
:
所以你得到的类型store.get(e)
是... Component
:
Component result = store.get(e);
When you cast Component
to T
, the compiler issues a warning because the cast cannot be checked statically. But if you're sure of the semantics of your data structure, you can simply suppress the warning.
当您强制转换Component
为 时T
,编译器会发出警告,因为无法静态检查强制转换。但是,如果您确定数据结构的语义,则可以简单地取消警告。
@SuppressWarnings("unchecked")
T resultT = (T)result;
PS: You don't need a wildcard capture, the following will work exactly the same in your case:
PS:您不需要通配符捕获,以下在您的情况下将完全相同:
private HashMap<Class<?>, HashMap<Entity, Component>> m_componentStores;