Generics of Java 1.5 Tiger - Checking for Lint
(Page 6 of 10 )
Several times in this chapter, you’ve heard about lint warnings, which sounds more like something you get out of a dryer than a compiler. These warnings are a new feature of Tiger, though, and important in figuring out how to really bulletproof your code.
How do I do that?
Take a simple piece of code that used a type that can be parameterized, but without type parameters:
private List getList() {
List list = new LinkedList();
list.add(3);
list.add("Blind");
list.add("Mice");
return list;
}
If you compile this in Tiger, with the -source 1.5 flag, you’ll get this message:
Note: GenericsTester.java uses unchecked or unsafe operations.
Note: recompile with -Xlint:unchecked for details.
You can compile all
of the examples
for this book with
“-Xlint:unchecked”
by using the Ant
target “compile-
check”.
If you recompile with the suggested flag, you are telling the compile to
show lint warnings (-Xlint), and specifically to show those warnings that are unchecked. .
Here’s some sample output with these warnings turned on:
[javac] code\src\com\oreilly\tiger\ch02\GenericsTester.java:63: warning:
[unchecked] unchecked call to add(E) as a member of
the raw type java.util.List
[javac] list.add(3);
[javac] ^
[javac] src\com\oreilly\tiger\ch02\GenericsTester.java:64: warning:
[unchecked] unchecked call to add(E) as a member of
the raw type java.util.List
[javac] list.add("Blind");
[javac] ^
[javac] src\com\oreilly\tiger\ch02\GenericsTester.java:65: warning:
[unchecked] unchecked call to add(E) as a member of
the raw type java.util.List
[javac] list.add("Mice");
[javac] ^
[javac] 3 warnings
These warnings indicate that the compiler isn’t able to ensure that the values added to the list (named list in this case) are the intended type. That’s because list wasn’t parameterized.
You can get rid of these warnings by specifying a type in your List construction:
private List getList() {
List<Object> list = new LinkedList<Object>();
list.add(3);
list.add("Blind");
list.add("Mice");
return list;
}
Autoboxing is
covered in
Chapter 4.
While this doesn’t do much for type-safety, it does take care of the warnings, as the types being added to list are all of type Object (the literal 3 is autoboxed to an Integer, which is of course an Object).
What about...
...annotations? For those of you who may be ahead on Tiger, there is an annotation, called SuppressWarnings, which allows you to keep these warnings from showing up in a compilation using -source 1.5. You can also just recompile under Java 1.4, although that’s obviously a pretty shortsighted solution. The best choice, if at all possible, is to parameterize your generic types, and enforce type-safety whenever possible.
Annotations are
covered in
Chapter 6.
Next: Generics and Type Conversions >>
More Java Articles
More By O'Reilly Media
|
This article was taken from chapter two of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388). Check it out at your favorite bookstore. Buy this book now.
|
|