suppressing warnings this month
By john.rose on Dec 02, 2011
Two questions came up during reviews, one about the equivalence of wildcards and named type parameters, and one about the best way to handle creation of an array of generic element types.
Here's the quick summary of how to write arrays of generics:
In the common case where desired type parameter is wild, there is no need to suppress warnings:@SuppressWarnings("unchecked") // array creation must have wildcard List<String> lss = (List<String>) new List<?>;
List<?> lqs = new List<?>; Class<?> cs = new Class<?>;
Note that every use of
@SuppressWarnings should have a comment.
@SuppressWarnings should be placed on the smallest
possible program element, usually a local variable declaration.
Here's an example of removing a deprecation warning. A typical deprecation warning is:
Here a the corresponding fix:Foo.java:876: warning: [deprecation] String(byte,int) in String has been deprecated String x = new String(new byte, 0); ^
The clarifying comment is a useful way to record any extra information (if known) why the deprecated method is being used.@SuppressWarnings("deprecation") // String(byte,int) in String has been deprecated // and this is actually useful because the fremdish preskittler was exsufflated String x = new String(new byte, 0);