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?
NOTE
You 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.
NOTE
An example of a warning that should be fixed is not handling all enumerated type values in a "switch" statement.
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:
/** * Normal pre-Tiger method body */ @SuppressWarnings(value={"unchecked"}) public void nonGenericsMethod( ) { List wordList = new ArrayList( ); wordList.add("foo"); }
NOTE
This code is in com. 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: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List [javac] wordList.add("foo"); [javac] ^
Of course, this has no business showing up if you truly are merely trying to target a pre-Tiger platform, and it's here that SuppressWarnings really comes into play. You can also specify multiple warnings to ignore:
/** * Normal pre-Tiger method body */ @SuppressWarnings(value={"unchecked", "fallthrough"}) public void nonGenericsMethod( ) { List wordList = new ArrayList( ); wordList.add("foo"); }
In fact, there's one more notation step you can takeannotation types 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:
NOTE
Curly braces are used anytime a member takes an array of values.
/** * Normal pre-Tiger method body */ @SuppressWarnings({"unchecked", "fallthrough"}) public void nonGenericsMethod( ) { List wordList = new ArrayList( ); wordList.add("foo"); }
This is a nice keystroke saver, at least in my book. The compiler is smart enough to route these values to the annotation type's single member, value.