6.4 Suppressing Warnings
With the advent of Tiger, there
are times when pre-Tiger code works
exactly as it should, but generates warnings. This is most often the case
in collections, as Tiger allows much stronger typing, and in fact pushes
you to code that way. However, thoroughly tested code should never
issue or generate warnings, creating a bit of a catch 22. Your code works
great, but causes warnings in Tigeron the other hand, ignoring all
warnings in a program isn't a good idea, either. How do you deal with
this situation?NOTEYou can view
warnings from the
Java compiler
with the "-Xlint"
switch.The answer is to use another standard annotation type,
SuppressWarnings, which lets you turn off warnings for a particular
class, method, or field/variable initializer. At the same time, warnings
for other pieces of code are left intact, as they should be.NOTEAn example of a
warning that
should be fixed is
not handling all
enumerated type
values in a "switch"
statement.
6.4.1 How do I do that?
SuppressWarnings is not a marker interface, as Deprecated and Override
are, but instead has a single member named value. value is a String
array (String[]), and the value of this member is an array of the types of
warnings to be suppressed. So, you could suppress unchecked warnings
with the following annotation:
/**NOTEThis code is in com.
* Normal pre-Tiger method body
*/
@SuppressWarnings(value={"unchecked"})
public void nonGenericsMethod( ) {
List wordList = new ArrayList( );
wordList.add("foo");
}
oreilly.tiger.ch06.
SuppressWarnings
Tester.If you're used to looking at Tiger code, you may see something wrong
herebecause generics aren't used, this generates an unchecked warning
(at least, without the SuppressWarnings annotation) because
wordList is untyped. However, this warning disappears with the annotationparticularly useful if you're compiling under Tiger, but targeting a
pre-Tiger platform.If you remove the annotation, you can see the warning that this code
generates:
[javac] src\ch06\SuppressWarningsTester.java:15:Of course, this has no business showing up if you truly are merely trying
warning: [unchecked] unchecked call to add(E) as a member of the
raw type java.util.List
[javac] wordList.add("foo");
[javac] ^
to target a pre-Tiger platform, and it's here that SuppressWarnings really
comes into play. You can also specify multiple warnings to ignore:
/**In fact, there's one more notation step you can takeannotation types
* Normal pre-Tiger method body
*/
@SuppressWarnings(value={"unchecked", "fallthrough"})
public void nonGenericsMethod( ) {
List wordList = new ArrayList( );
wordList.add("foo");
}
that only have one member will automatically pass all values through to
that member, as long as that member is named value. As a result, you
can omit the value= portion of the declaration:NOTECurly braces are
used anytime a
member takes an
array of values.
/**This is a nice keystroke saver, at least in my book. The compiler is smart
* Normal pre-Tiger method body
*/
@SuppressWarnings({"unchecked", "fallthrough"})
public void nonGenericsMethod( ) {
List wordList = new ArrayList( );
wordList.add("foo");
}
enough to route these values to the annotation type's single member,
value.