类型安全:未经检查的强制转换 [英] Type safety: Unchecked cast

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

问题描述

在我的 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:

类型安全:从 Object 到 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.

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(如果您愿意的话).因此,不能保证当你得到你的 bean 时它可以表示为 HashMap 因为你可以有 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.

如果代码编译通过,并且您可以执行 String value = 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天全站免登陆