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
@SuppressWarnings
here, there's no other option anyway", and whereCan 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?
The previous examples did not actually provoke the warnings. Some answers no longer make sense now. Sorry for the inconvenience.
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.
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 .
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With