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.