类型安全:未经检查的演员 [英] Type safety: Unchecked cast

查看:129
本文介绍了类型安全:未经检查的演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的spring应用程序上下文文件中,我有类似的内容:

In my spring application context file, I have something like:

<util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String">
    <entry key="some_key" value="some value" />
    <entry key="some_key_2" value="some value" />   
</util:map>

在java类中,实现如下:

In java class, the implementation looks like:

private Map<String, String> someMap = new HashMap<String, String>();
someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");

在Eclipse中,我看到一条警告:

In Eclipse, I see a warning that says:

类型安全:未选中从对象转换为HashMap

我做错了什么?我该如何解决这个问题呢?

What did I do wrong? How do I resolve the issue?

推荐答案

首先,你用新的<$ c $浪费记忆力c> HashMap 创建调用。您的第二行完全忽略了对此创建的hashmap的引用,使其可用于垃圾收集器。所以,不要那样做,使用:

Well, first of all, you're wasting memory with the new HashMap creation call. Your second line completely disregards the reference to this created hashmap, making it then available to the garbage collector. So, don't do that, use:

private Map<String, String> someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");

其次,编译器抱怨你将对象转换为 HashMap 而不检查它是否是 HashMap 。但是,即使您要这样做:

Secondly, the compiler is complaining that you cast the object to a HashMap without checking if it is a HashMap. But, even if you were to do:

if(getApplicationContext().getBean("someMap") instanceof HashMap) {
    private Map<String, String> someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");
}

您可能仍会收到此警告。问题是, getBean 返回 Object ,因此不知道该类型是什么。直接将它转换为 HashMap 不会导致第二种情况出现问题(也许在第一种情况下不会出现警告,我不确定Java是多么迂腐编译器带有Java 5的警告。但是,您将它转换为 HashMap< String,String>

You would probably still get this warning. The problem is, getBean returns Object, so it is unknown what the type is. Converting it to HashMap directly would not cause the problem with the second case (and perhaps there would not be a warning in the first case, I'm not sure how pedantic the Java compiler is with warnings for Java 5). However, you are converting it to a HashMap<String, String>.

HashMaps实际上是一个地图对象作为键,并将对象作为值, HashMap< Object,Object> 如果愿意的话。因此,无法保证当你获得bean时它可以表示为 HashMap< String,String> ,因为你可以拥有 HashMap<日期,日历> 因为返回的非泛型表示可以包含任何对象。

HashMaps are really maps that take an object as a key and have an object as a value, HashMap<Object, Object> if you will. Thus, there is no guarantee that when you get your bean that it can be represented as a HashMap<String, String> because you could have HashMap<Date, Calendar> because the non-generic representation that is returned can have any objects.

如果代码编译,则可以执行字符串值= map.get(thisString); 没有任何错误,请不要担心此警告。但是如果映射不完全是字符串键的字符串键,那么在运行时将得到 ClassCastException ,因为在这种情况下泛型不能阻止这种情况发生。

If the code compiles, and you can execute String value = map.get("thisString"); without any errors, don't worry about this warning. But if the map isn't completely of string keys to string values, you will get a ClassCastException at runtime, because the generics cannot block this from happening in this case.

这篇关于类型安全:未经检查的演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆