How do I address unchecked cast warnings? How do I address unchecked cast warnings? java java

How do I address unchecked cast warnings?


The obvious answer, of course, is not to do the unchecked cast.

If it's absolutely necessary, then at least try to limit the scope of the @SuppressWarnings annotation. According to its Javadocs, it can go on local variables; this way, it doesn't even affect the entire method.

Example:

@SuppressWarnings("unchecked")Map<String, String> myMap = (Map<String, String>) deserializeMap();

There is no way to determine whether the Map really should have the generic parameters <String, String>. You must know beforehand what the parameters should be (or you'll find out when you get a ClassCastException). This is why the code generates a warning, because the compiler can't possibly know whether is safe.


Unfortunately, there are no great options here. Remember, the goal of all of this is to preserve type safety. "Java Generics" offers a solution for dealing with non-genericized legacy libraries, and there is one in particular called the "empty loop technique" in section 8.2. Basically, make the unsafe cast, and suppress the warning. Then loop through the map like this:

@SuppressWarnings("unchecked")Map<String, Number> map = getMap();for (String s : map.keySet());for (Number n : map.values());

If an unexpected type is encountered, you will get a runtime ClassCastException, but at least it will happen close to the source of the problem.


Wow; I think I figured out the answer to my own question. I'm just not sure it's worth it! :)

The problem is the cast isn't checked. So, you have to check it yourself. You can't just check a parameterized type with instanceof, because the parameterized type information is unavailable at runtime, having been erased at compile time.

But, you can perform a check on each and every item in the hash, with instanceof, and in doing so, you can construct a new hash that is type-safe. And you won't provoke any warnings.

Thanks to mmyers and Esko Luontola, I've parameterized the code I originally wrote here, so it can be wrapped up in a utility class somewhere and used for any parameterized HashMap. If you want to understand it better and aren't very familiar with generics, I encourage viewing the edit history of this answer.

public static <K, V> HashMap<K, V> castHash(HashMap input,                                            Class<K> keyClass,                                            Class<V> valueClass) {  HashMap<K, V> output = new HashMap<K, V>();  if (input == null)      return output;  for (Object key: input.keySet().toArray()) {    if ((key == null) || (keyClass.isAssignableFrom(key.getClass()))) {        Object value = input.get(key);        if ((value == null) || (valueClass.isAssignableFrom(value.getClass()))) {            K k = keyClass.cast(key);            V v = valueClass.cast(value);            output.put(k, v);        } else {            throw new AssertionError(                "Cannot cast to HashMap<"+ keyClass.getSimpleName()                +", "+ valueClass.getSimpleName() +">"                +", value "+ value +" is not a "+ valueClass.getSimpleName()            );        }    } else {        throw new AssertionError(            "Cannot cast to HashMap<"+ keyClass.getSimpleName()            +", "+ valueClass.getSimpleName() +">"            +", key "+ key +" is not a " + keyClass.getSimpleName()        );    }  }  return output;}

That's a lot of work, possibly for very little reward... I'm not sure if I'll use it or not. I'd appreciate any comments as to whether people think it's worth it or not. Also, I'd appreciate improvement suggestions: is there something better I can do besides throw AssertionErrors? Is there something better I could throw? Should I make it a checked Exception?