suppressing warnings this month

The OpenJDK community just had a nice sprint cleaning up warnings in JDK code. I did my bit; it was fun.

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:

@SuppressWarnings("unchecked")  // array creation must have wildcard
List<String>[] lss = (List<String>[]) new List<?>[1];
In the common case where desired type parameter is wild, there is no need to suppress warnings:
List<?>[] lqs = new List<?>[1];
Class<?>[] cs = new Class<?>[1];

Note that every use of @SuppressWarnings should have a comment. Also, every @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:

Foo.java:876: warning: [deprecation] String(byte[],int) in String has been deprecated
String x = new String(new byte[0], 0);
           ^
Here a the corresponding fix:
@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], 0);
The clarifying comment is a useful way to record any extra information (if known) why the deprecated method is being used.
Comments:

About the equivalence relations between named parameter types and wildcards. The JLS rule them out because it requires that the two overrides must have the same number of named parameter types. But theoretically, you can override a wildcard with a named parameter.

Rémi

Posted by Rémi Forax on December 03, 2011 at 10:12 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

John R. Rose

Java maven, HotSpot developer, Mac user, Scheme refugee.

Once Sun and present Oracle engineer.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today