Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not simply disable unchecked warnings?

When developers interact with non-generic APIs, they usually run into "unchecked" warnings. Consider the following example:

import java.util.AbstractList;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class IterableNodeList<T extends Node> extends AbstractList<T>
{
    private NodeList list;

    public IterableNodeList(NodeList list)
    {
        this.list = list;
    }

    public T get(int index)
    {
        return (T)this.list.item(index);
    }

    public int size()
    {
        return this.list.getLength();
    }
}

One could of course invest the effort to write this in such a way that there is no warning: using a type parameter T on the class and a constructor argument Class<T>, matching member variable and a cast() call.

Alternatively, one could think about simply editing the IDE configuration and build scripts (e.g. Maven POM) to disable this compiler warning entirely. Now if we did that, the code could stay the way it is, but I'm sure that doing must have drawbacks. However, I can't think of any reasonable, realistic examples where

  • this warning provides more value than "stick on a @SuppressWarnings here, there's no other option anyway", and where
  • the resulting code actually behaves different (and safer) than the one where we ignored (disabled) the warning.

Can you think of such examples or name another reason why disabling these "unchecked" warnings globally is a bad idea? Or is it actually a good idea?

UPDATE

The previous examples did not actually provoke the warnings. Some answers no longer make sense now. Sorry for the inconvenience.

like image 433
Jens Bannmann Avatar asked Feb 05 '13 12:02

Jens Bannmann


People also ask

How to suppress unchecked warnings in Java?

You may just use @SuppressWarnings(“unchecked”)to suppress unchecked warnings in Java. 1. In Class If applied to class level, all the methods and members in this class will ignore the unchecked warnings message.

What does the “unchecked cast” warning mean in Java?

Some Java compilers suppress unchecked warnings by default. Let's make sure we've enabled the compiler's option to print “unchecked” warnings before we look into this “ unchecked cast ” warning. 2. What Does the “unchecked cast” Warning Mean? The “unchecked cast” is a compile-time warning .

How do I get rid of the unchecked cast warning?

Suppress the “ unchecked ” Warning If we can't eliminate the “ unchecked cast ” warning and we're sure that the code provoking the warning is typesafe, we can suppress the warning using the SuppressWarnings (“unchecked”) annotation. When we use the @SuppressWarning (“unchecked”) annotation, we should always put it on the smallest scope possible.

Does Java use Unchecked or unsafe operations?

Finally, in most circumstances, you can simply disregard it. To conclude that Java uses unchecked or unsafe operations is not a serious error, it is just simply a warning. So, if you get it, don’t worry, all you need is to apply the solution we instructed above.


1 Answers

According to Item 24 of Effective Java 2nd Edition, widely and frequent using @SupressWarnings is generally bad idea, especially if you apply this annotation to the whole class, because such warnings show you possibly dangerous pieces of your code, which could lead to ClassCastException.

But in some case it could be useful, for example in ArrayList's toArray method implementation:

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
    if (a.length < size)
        // Make a new array of a's runtime type, but my contents:
        return (T[]) Arrays.copyOf(elementData, size, a.getClass());
    System.arraycopy(elementData, 0, a, 0, size);
    if (a.length > size)
        a[size] = null;
    return a;
}
like image 186
Andremoniy Avatar answered Sep 22 '22 15:09

Andremoniy