Java shows "Type Safety: Unchecked cast from Object to Hashmap"

Asked

Viewed 2,230 times

3

I’m creating a class that through that works similar to Xpath.

The function works perfectly, the problem is that in this part of the line (HashMap<String, Object>) test.get("test-map");, "eclipse" shows the following warning:

Type Safety: Unchecked cast from Object to Hashmap

I believe it’s my fault, the way I’m working may be wrong.

How can I solve?

HashMap<String, Object> test = new HashMap<String, Object>();
test.put("test-string", "abc");
boolean isNull = test.get("test-map")==null;

if(isNull==false){
    HashMap<String, Object> map1 = (HashMap<String, Object>) test.get("test-map");
    System.out.println(map1);
    isNull = map1==null;
}

if(isNull){
    test.put("test-map", new HashMap<String, Object>());
}
String map2 = (String) test.get("test-string");

System.out.println(map2);

3 answers

3

It’s a standard Java Warning when doing cast for a guy who uses Generics.

In Eclipse, you can remove Warning by annotating the variable declaration with:

@SuppressWarnings("unchecked")
HashMap<String, Object> map1 = (HashMap<String, Object>) test.get("test-map");

This Warning happens because the compiler always ensures that code you use Generics will not give ClassCastException. But how are you converting from a Object, that he has no way of knowing what it is, he gives this warning saying that he does not guarantee.

  • So I’ve done this, but I don’t believe this is the best.

  • You have 2 alternatives, either you change your map to instead of being string to Object to be string to hashmap<...> (which may not be possible) or you guarantee via programming logic that at that moment you are giving the cast what you have there is actually a hashmap<...>. And if via logic is guaranteed, you can suppress the Warning.

  • I agree, but what I need to know is: is this really the best way to do this? I refer to my code.

  • I’d say you’re the only one. By a choice of java design, information about Hashmap generic arguments (and any class that has type arguments) is not preserved at runtime. The execution environment will know that it is a Hashmap, but will not know what it is. So it will always give Warning when you force a cast to a type with Generics. There is an option to declare the variable as Hashmap<? ,? >, which is safe and does not give warnings, but prevents you from using the map.

  • You can change your logic, so that besides the Object in the map you also store an identifier of what type of object was saved, it can be an Enum. This way, when removing the object you can make a check and be sure of the type of the object. And so you can place the Warning deletion annotation knowing that you are doing everything possible.

  • Just to conclude, my code is good, or I should use instanceof ?

  • instanceof It won’t solve the Warning problem, so it’s useless to use it.

  • With the instanceof you can at most test whether the content of the object is a HashMap, but you won’t be able to tell the difference between a HashMap<A,B> and a HashMap<C,D>, because that information is lost at runtime. If you want to be absolutely sure what you have in the map you will need to store this information, for example as a enum, as I said. But this ties more architecture, for every new type you want to put you will have to add a new entry in Enum.

Show 3 more comments

0

Apply the following Annotation in your method:

@SuppressWarnings("unchecked")

That way the eclipse won’t signal this Warning.

  • 2

    It is best to apply the annotation only to the variable declaration. If applying this Warning method will be ignored in the whole, which may make some error escape.

  • @C.E.Gesser I agree.

  • A good observation.

-1

Eclipse launches this warning because you are casting without testing the type of the object. to avoid this, you could do the following:

if(test.get("test_map") instanceof HashMap<String, Object>){
    HashMap<String, Object> map1 = (HashMap<String, Object>) test.get("test-map");
}
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.